首页 > 解决方案 > 如何在 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"
        }
    ]
}]

标签: javascriptphpjson

解决方案


您应该将 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)
}

推荐阅读