我有多维数组。
$array = [
"player" => [
"@attributes" => [
"id" => 0,
"avatar" => "MALE"
],
"scenarios" => [
"scenario" => [
0 => [
"@attributes" => [
"id" => 1,
"attempts" => 1,
"score" => 7,
"completed" => 1,
"time" => "00:00:10"
],
"badges" => "4,1"
],
1 => [
"@attributes" => [
"id" => 2,
"attempts" => 4,
"score" => 0,
"completed" => 0,
"time" => "00:00:10"
],
"badges" => "3, 4"
],
2 => [
"@attributes" => [
"id" => 3,
"attempts" => 2,
"score" => 10,
"completed" => 0,
"time" => "00:00:10"
],
"badges" => "2, 2"
],
3 => [
"@attributes" => [
"id" => 4,
"attempts" => 5,
"score" => 30,
"completed" => 0,
"time" => "00:00:10"
],
"badges" => "1,1"
]
]
]
]
];
我必须将此数据插入数据库。
我试过这段代码
outputValue($array);
function outputValue($array){
foreach($array as $key => $value){
if(is_array($value)){
outputValue($value);
continue;
}
else{
queryFire($array);
}
}
}
function queryFire($array){
global $id, $attempts, $scores, $time, $lastElement;
foreach($array as $key => $value){
switch ($key){
case 'id':
$id = $value;
case 'attempts':
$attempts = $value;
case 'score':
$scores = $value;
case 'time':
$time = $value;
}
}
echo $query = "INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('$id', '$attempts', '$scores', '$time')";
echo '<br>';
}
但它给了我
INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('0', '0', '0', '0')
INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('0', '0', '0', '0')
INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('1', '1', '7', '00:00:10')
INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('1', '1', '7', '00:00:10')
INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('1', '1', '7', '00:00:10')
INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('1', '1', '7', '00:00:10')
INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('1', '1', '7', '00:00:10')
INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('1', '1', '7', '00:00:10')
INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('2', '4', '0', '00:00:10')
INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('2', '4', '0', '00:00:10')
INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('2', '4', '0', '00:00:10')
INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('2', '4', '0', '00:00:10')
我不想要...我希望查询执行一次。没有重复。
结果应为
`INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('0', '0', '0', '0')
INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('1', '1', '7', '00:00:10')
INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('2', '4', '0', '00:00:10')
我可以在foreach循环或任何其他技术之外编写查询吗?
答案 0 :(得分:2)
我会拿你的数组,然后像这样做一个简单的迭代:
pdo = Database::connect();
$sql = 'SELECT orders.animal, orders.size, storage.name, storage.space FROM target LEFT JOIN orders ON storage.name=orders.animal;';
foreach ($pdo->query($sql) as $row) {
$count+ = $row['size'];
$result = $row['space'] - $count;
if ($result == 0) {
echo'
<tr>
<td>'.$row['name'].'</td>
<td>'.$row['space'].'</td>
</tr>
';
} else {
echo'
<tr>
<td>'.$row['name'].'</td>
<td>'.$result.'</td>
</tr>
';
}
Database::disconnect();
<强>结果:强>
$q = "INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`) VALUES ";
foreach ($array["player"]["scenarios"]["scenario"] as $key => $value) {
$v = $value["@attributes"];
$q .= "(`{$v['id']}`, `{$v['completed']}`, `{$v['score']}`, `{$v['attempts']}`),";
}
echo rtrim($q, ',');
答案 1 :(得分:0)
试用以下代码。在这里,我试图将字符串附加在变量
中$查询
单插入。因此DB只能访问一次。
$query = "";
outputValue($array);
function outputValue($array){
foreach($array as $key => $value){
if(is_array($value)){
outputValue($value);
continue;
}else{
queryFire($array);
}
}
}
function queryFire($array){
global $id, $attempts, $scores, $time, $lastElement;
foreach($array as $key => $value){
switch ($key){
case 'id':
$id = $value;
case 'attempts':
$attempts = $value;
case 'score':
$scores = $value;
case 'time':
$time = $value;
}
}
if($query=="")
{
$query = "INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES('$id', '$attempts', '$scores', '$time')";
}
$query .= ",('$id', '$attempts', '$scores', '$time')";
}
echo $query;
答案 2 :(得分:0)
您可以创建查询的第一部分(INSERT INTO tbluserscenescores
(suid
,completed
,score
,attempts
)VALUES)并添加此项到一个字符串。
然后通过循环追加值以生成单个(尽管是大量的)SQL查询。
<?php
outputValue($array);
function outputValue($array){
foreach($array as $key => $value){
if(is_array($value)){
outputValue($value);
continue;
}
else{
queryFire($array);
}
}
}
function queryFire($array){
global $id, $attempts, $scores, $time, $lastElement;
$query = "INSERT INTO `tbluserscenescores`(`suid`, `completed`, `score`, `attempts`)VALUES";
foreach($array as $key => $value){
switch ($key){
case 'id':
$id = $value;
case 'attempts':
$attempts = $value;
case 'score':
$scores = $value;
case 'time':
$time = $value;
}
$query .= "('$id', '$attempts', '$scores', '$time'),";
}
echo $query;
}
?>