有人知道如何在PHP中循环使用此JSON数组吗?我只是尝试过,但是没有用
[{
//
"data": [{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "2",
"name": "Garrett Winters",
"position": "Accountant",
"salary": "$170,750",
"start_date": "2011/07/25",
"office": "Tokyo",
"extn": "8422"
},
{
"id": "3",
"name": "Ashton Cox",
"position": "Junior Technical Author",
"salary": "$86,000",
"start_date": "2009/01/12",
"office": "San Francisco",
"extn": "1562"
},
{
"id": "4",
"name": "Cedric Kelly",
"position": "Senior Javascript Developer",
"salary": "$433,060",
"start_date": "2012/03/29",
"office": "Edinburgh",
"extn": "6224"
}
]
}]
答案 0 :(得分:1)
您应将json转换为数组,然后将每个json对象转换为数组
$json = "current Json data";
$json = json_decode($json); //convert json to array
$json = $json[0]->data; //get all data
foreach ($json as $key => $value) {
$value = get_object_vars($value); // convert object to array
print_r($value)
}
答案 1 :(得分:0)
首先,将您的JSON解码为PHP数组,然后您可以照常遍历它。
$arrayItems = json_decode($jsonString)
foreach ($arrayItems as $item){
// do something
}
答案 2 :(得分:0)
您可以使用以下代码来迭代json格式:
$jsonData = json_decode($data);
foreach($jsonData as $jsonDataKey => $jsonDataValue){
foreach($jsonDataValue as $jsonArrayKey => $jsonArrayValue){
echo $jsonArrayValue['id'];
echo $jsonArrayValue['name'];
echo $jsonArrayValue['position'];
}
}