我有流入数据库的json数据。数据总是在变化并且在增加,所以我需要考虑到这一点。 我需要抓取一些嵌套在数组中的数据。这是json的一小部分:
"code":3,
"data":[
{
"evaluator_id":"1",
"subject":{
"lastName":"John",
"firstName":"Smith",
},
"data":{
"task1Data":[
{
"fixedObjectsPresented":11,
},
{
"fixedObjectsPresented":10,
},
{
"fixedObjectsPresented":9,
},
{
"fixedObjectsPresented":8,
},
{
"fixedObjectsPresented":7,
},
{
"fixedObjectsPresented":6,
},
{
"fixedObjectsPresented":5,
},
{
"fixedObjectsPresented":4,
},
{
"fixedObjectsPresented":3,
},
{
"fixedObjectsPresented":2,
}
]
},
"organization":"Tester",
"evaluator":"Tester1"
}
]
我试图抓住fixedObjectsPresented字段,我可以成功完成。问题是它只抓取我创建的每个用户的第一行。 在这种情况下,有10行,因此应在数据库表中创建每个用户10行。现在它只抓住我拥有的每个用户的第一行。
这是我的php代码:
<?php
// connect to the database
$con = mysql_connect("127.0.0.1","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("Test", $con);
mysql_set_charset('utf8',$con);
// read the json file contents
$jsondata = file_get_contents('hidden.json');
// convert json object to php associative array
$data = json_decode($jsondata, true);
$count=0;
foreach($data['data'] as $row) {
// set the variables
$task = $row['data']['task1Data'];
// able to grab any single row with [0], but need to grab all rows
$fixedObjectsPresented = $task['fixedObjectsPresented'];
$sql = "INSERT INTO example(fixedObjectsPresented)
VALUES('$fixedObjectsPresented')
ON DUPLICATE KEY UPDATE fixedObjectsPresented = fixedObjectsPresented";
$result = mysql_query($sql);
if($result) {
$count++;
}
// todo change to !result when fixed
else if($result){
die('Error : ' . mysql_error());
}
}
return $count;
?>
我也试过了一个嵌套的foreach语句,但我得到了相同的结果。有谁知道怎么做?
答案 0 :(得分:0)
foreach($task as $fixedObjectsPresented) {
// able to grab any single row with [0], but need to grab all rows
$sql = "INSERT INTO example(fixedObjectsPresented)
VALUES('$fixedObjectsPresented')
ON DUPLICATE KEY UPDATE fixedObjectsPresented = fixedObjectsPresented";
$result = mysql_query($sql);
}
答案 1 :(得分:0)
听起来你只需要遍历你的$ task数组。见下面的代码:
// connect to the database
$con = mysql_connect("127.0.0.1","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("Test", $con);
mysql_set_charset('utf8',$con);
// read the json file contents
$jsondata = file_get_contents('hidden.json');
// convert json object to php associative array
$data = json_decode($jsondata, true);
$count=0;
echo "<pre>";
foreach($data['data'] as $row) {
// set the variables
$task = $row['data']['task1Data'];
if (count($task)) {
foreach($task as $subtask) {
$fixedObjectsPresented = $subtask['fixedObjectsPresented'];
$sql = "INSERT INTO example(fixedObjectsPresented)
VALUES('$fixedObjectsPresented')
ON DUPLICATE KEY UPDATE fixedObjectsPresented = fixedObjectsPresented";
$result = mysql_query($sql);
if($result) {
$count++;
}
// todo change to !result when fixed
else if($result) {
die('Error : ' . mysql_error());
}
}
}
}
return $count;
答案 2 :(得分:0)
你的代码正在运行,但你的逻辑在这里有点不对
foreach($data['data'] as $row) {
// set the variables
$task = $row['data']['task1Data'];//that will get in Array
//foreach will able to grab any single row with [0] of all rows
foreach ($tab as $key => $value) {
$fixedObjectsPresented = $tab[$key]['fixedObjectsPresented'];
$sql = "INSERT INTO example(fixedObjectsPresented)
VALUES('$fixedObjectsPresented')
ON DUPLICATE KEY UPDATE fixedObjectsPresented = fixedObjectsPresented";
$result = mysql_query($sql);
if($result) {
$count++;
}
// todo change to !result when fixed
else if($result){
die('Error : ' . mysql_error());
}
}
}