如何在php中获取json数据的属性


How to get attribute of a json data in php

我有如下的示例json数据

[{"@attributes":{"id":"F11D-6T47"},"Name":{"FirstName":"Praveen",
"MiddleName":{},"LastName":"Indukuri","NameSuffix":{}},
"DateOfBirth":{"Day":"31","Month":"08","Year":"1990"},"Phone":"4047549241",
"SSN":"670249778","DriversLicenseNumber":"I532671903110",
"Address":{"Line1":"144 Towey Trai","Line2":{},"Line3":{},
"City":"Woodstock","State":"Georgia","Zip":"30188","County":{},
"Latitude":"0","Longitude":"0"}},{"@attributes":{"id":"F11D-6T47"},
"Name":{"FirstName":"Praveen","MiddleName":{},"LastName":"Indukuri",
"NameSuffix":{}},"DateOfBirth":{"Day":"31","Month":"08","Year":"1990"},
"Phone":"4047549241","SSN":"670249778","DriversLicenseNumber":"I532671903110",
"Address":{"Line1":"144 Towey Trai","Line2":{},"Line3":{},
"City":"Woodstock","State":"Georgia","Zip":"30188",
"County":{},"Latitude":"0","Longitude":"0"}}]

我想使用php获取enitre数组的@attributes中的所有id值。

将json分配给一个变量,比如$json,并对其进行解码以获得php数组。

$array = json_decode($json, true);
// array to store the ids.
$ids = array();
foreach($array as $item) {
   $attributes = $item['@attributes'];
   array_push($ids, $attributes['id']);
}

希望这能有所帮助。