来自响应的XML代码:
<hints label="luggage">20</hints>
<hints label="handluggage">5</hints>
<hints label="landing"></hints>
这个函数响应后:
$arrayResponse = json_decode(json_encode((array)simplexml_load_string($response)), true);
我有这个:
"hints" => [
0 => "20",
1 => "5",
2 => [
@attributes = [
label => "landing"
]
]
]
钥匙“行李箱”和“handluggage”不存在。
如何使用XML中的值获取KEYS?例:
[
"luggage" => 20,
"handluggage" => 5,
"landing" => null
]
不使用json
的解决方案:
$response = '<root><hints label="luggage">20</hints><hints label="handluggage">5</hints><hints label="landing"></hints></root>';
$a = [];
foreach (simplexml_load_string($response)->hints as $hint) {
$value = (string)$hint;
$a[(string)$hint['label']] = $value ?: null;
}
print_r($a);