我正在使用一个API,它在将对象转换为数组后返回一个对象,它提供了以下数组。如何从下面的数组中获取AirPortName
?
Array
(
[@attributes] => Array
(
[AirPortName] => Indira Gandhi
[CityName] => New Delhi
[CodeContext] => IATA
[LocationCode] => DEL
[TerminalID] => 1B
)
)
我使用以下代码将对象转换为数组
$array = json_decode(json_encode((array)$p), TRUE);
答案 0 :(得分:2)
如果您有对象作为键,那么您可以这样做:
foreach($your_array as $key => $values){
// This is how you get Airport name
echo $your_array->$key['AriportName'];
}
答案 1 :(得分:1)
将obj转换为数组后使用
echo $array['@attributes']['AirPortName'];
示例:
$array = array('@attributes' => array('AirPortName' => 'Indira Gandhi','CityName' => 'New Delhi','CodeContext' => 'IATA','LocationCode' => 'DEL','TerminalID' => '1B' ));
echo $array['@attributes']['AirPortName'];
会给你:
Indira Gandhi