javascript - 如何在 PHP 中循环 Json 数组
问题描述
有谁知道我如何在 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"
}
]
}]
解决方案
您应该将 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)
}
推荐阅读
- python - LSTM 隐藏状态维度存在错误:RuntimeError: Expected hidden[0] size (4, 1, 256), got (1, 256)
- amazon-web-services - 查询系统的实例mac地址
- google-app-maker - Google AppMaker Id(主键)在保存时丢失或为空
- c# - Streamreader 只返回“System.IO.StreamReader”
- php - 显示没有偏移的帖子
- c++ - 两段代码中哪一段返回的答案更准确?
- mysql - MySQL Galera 5.7 最大连接问题
- rabbitmq - 从 Beam/DataFlow 读取 RabbitMQ
- php - PHP包含在将页面移动到子文件夹后不起作用
- c# - Quartz.net 通过特定的作业数据获取所有作业