简体   繁体   English

将 SimpleXML 属性嵌套到关联数组

[英]Nested SimpleXML Attributes To Associative Array

I am integrating a payment gateway into a website and their API is returning an xml object where the values I require are nested.我正在将支付网关集成到网站中,他们的 API 正在返回一个 xml 对象,其中嵌套了我需要的值。

SimpleXMLElement Object
(

    [form] => SimpleXMLElement Object
        (
            [input] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [type] => hidden
                                    [name] => SessionStored
                                    [value] => True
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [type] => hidden
                                    [name] => SessionStoredError
                                    [value] => 
                                )

                        )

                    [2] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [type] => hidden
                                    [name] => SST
                                    [value] => e19e8abe-a2d6-4ce7
                                )

                        )

                )

        )

)

Using php how can I get the nested attributes into an associative array like the following format?使用 php 如何将嵌套属性放入如下格式的关联数组中?

$array['SessionStored'] = 'True'
$array['SessionStoredError'] = ''
$array['SST'] = 'e19e8abe-a2d6-4ce7'

Its a bit messy but after reading other articles online I have put together the following which throws a 'Fatal error: Call to a member function attributes()'它有点凌乱,但在阅读其他在线文章后,我整理了以下内容,这些内容会引发“致命错误:调用成员函数属性()”

$xmlData = simplexml_load_string($result);
$aXml = json_decode( json_encode($xmlData) , 1);

$testArray = $aXml['form']['input'];

for($i = 0; $i < count($testArray); $i++)
{
    foreach($testArray[$i]->attributes() as $a => $b) {
        echo $a,'="',$b,"\"\n";
    }
}

Do not try to convert the XML.不要尝试转换 XML。

Converting XML to JSON means loosing information.将 XML 转换为 JSON 意味着丢失信息。 Generic conversion does not use the semantic structure.泛型转换不使用语义结构。 You don't have "nested attributes" just some input element nodes with attribute nodes.您没有“嵌套属性”,只有一些带有属性节点的输入元素节点。

Read it and generate the array from the data.读取它并从数据生成数组。

$result = [];
$element = new SimpleXMLElement($xml);
foreach ($element->form->input as $input) {
  $result[(string)$input['name']] = (string)$input['value'];
}

var_dump($result);

Output:输出:

array(3) {
  ["SessionStored"]=>
  string(4) "True"
  ["SessionStoredError"]=>
  string(0) ""
  ["SST"]=>
  string(18) "e19e8abe-a2d6-4ce7"
}

This is easy with DOM, too:这对于 DOM 也很容易:

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$result = [];
foreach ($xpath->evaluate('//form/input') as $input) {
  $result[$input->getAttribute('name')] = $input->getAttribute('value');
}

var_dump($result);

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

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