简体   繁体   English

回显从 XML 生成的数组键的特定值

[英]Echo specific value of a key of an array generated from an XML

I have the following code:我有以下代码:

$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<note>
    <RESPONSE
        xmlns='http://www.ibm.com/maximo'
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' creationDateTime='2022-09-22T09:51:01+02:00' transLanguage='EN' baseLanguage='EN' messageID='993921663833062257153' maximoVersion='7 6 20180718-1141 V7610-83' rsStart='0' rsTotal='1' rsCount='1'>
        <CHANGE>
            <TEST>
                <CHANGEBY>TESTUSER</CHANGEBY>
                <CHANGEDATE>2022-09-21T17:42:21+01:00</CHANGEDATE>
                <CIA_DONE>YES</CIA_DONE>
            </TEST>
        </CHANGE>
    </RESPONSE>
</note>";

$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object");
$array = json_decode(json_encode((array)$xml), TRUE);
print_r($array);

That returns the following result:这将返回以下结果:

Array ( [RESPONSE] => Array ( [@attributes] => Array ( [creationDateTime] => 2022-09-22T09:51:01+02:00 [transLanguage] => EN [baseLanguage] => EN [messageID] => 993921663833062257153 [maximoVersion] => 7 6 20180718-1141 V7610-83 [rsStart] => 0 [rsTotal] => 1 [rsCount] => 1 ) [CHANGE] => Array ( [TEST] => Array ( [CHANGEBY] => TESTUSER [CHANGEDATE] => 2022-09-21T17:42:21+01:00 [CIA_DONE] => YES ) ) ) )

Now I want to echo the value of "CHANGEDATE", but echo $array["note"]["RESPONSE"]["CHANGE"]["TEST"]["CHANGEBY"];现在我想回显“CHANGEDATE”的值,但是echo $array["note"]["RESPONSE"]["CHANGE"]["TEST"]["CHANGEBY"]; returns nothing.什么都不返回。

There's no need to go to JSON, you can use your SimpleXML object directly:不需要 go 到 JSON,您可以直接使用您的 SimpleXML object:

echo $xml->RESPONSE->CHANGE->TEST->CHANGEBY;

However, if you really want/need to, the root element note needs to be skipped:但是,如果您真的想要/需要,则需要跳过根元素note

echo $array["RESPONSE"]["CHANGE"]["TEST"]["CHANGEBY"];

Demo: https://3v4l.org/fbTpB演示: https://3v4l.org/fbTpB

①如果本文未解决您的问题,请点击查看与本文相关的问题
②如果本文未解决您的问题,请向程序员专用AI小助手提问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM