-1

I am integrating a payment gateway into a website and their API is returning an xml object where the values I require are nested.

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?

$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";
    }
}
Jamesil
  • 173
  • 1
  • 5
  • 17

1 Answers1

2

Do not try to convert the XML.

Converting XML to JSON means loosing information. 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:

$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);
ThW
  • 19,120
  • 3
  • 22
  • 44