响应中的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"
]
]
]
键“行李”和“行李”不存在。
如何使用XML值获取 KEYS ? 示例:
[
"luggage" => 20,
"handluggage" => 5,
"landing" => null
]
答案 0 :(得分:1)
不使用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);