来自curl响应的SimpleXMLElement - @attributes的问题

问题描述 投票:0回答:1

来自响应的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
]
php attributes simplexml simplexml-load-string
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.