I'm trying to convert XML which has namespaces, attributes to array by using JsonSerializable
& iterator_to_array()
as suggested by @hakre here:
https://hakre.wordpress.com/2013/07/10/simplexml-and-json-encode-in-php-part-iii-and-end/
The issue I'm facing is when a node in the XML has just one child. Here's a description of the problem:
<node1 attr1="val1" attr2="val2>
<node2 attr3="val3" attr4="val4" />
<node2 attr3="val3" attr4="val4" />
<node2 attr3="val3" attr4="val4" />
</node1>
So the array would be
[node1] => Array
(
[@attributes] => Array
(
[attr1] => val1
[attr2] => val2
)
[node2] => Array
(
[0] => Array
(
[@attributes] => Array
(
[attr3] => val3
[attr4] => val4
)
)
[1] => Array
(
[@attributes] => Array
(
[attr3] => val3
[attr4] => val4
)
)
[2] => Array
(
[@attributes] => Array
(
[attr3] => val3
[attr4] => val4
)
)
)
)
So I'd use something like:
foreach($array['node1']['node1'] AS $node){
print_r($node['@attributes']);
}
But some times...
<node1 attr1="val1" attr2="val2>
<node2 attr3="val3" attr4="val4" />
</node1>
Then array be like:
[node1] => Array
(
[@attributes] => Array
(
[attr1] => val1
[attr2] => val2
)
[node2] => Array
(
[@attributes] => Array
(
[attr3] => val3
[attr4] => val4
)
)
)
In that case the foreach
would fail because @attributes
is direct child of node2
unlike the first case node2[0]['@attributes']
.
The XML is a webservice response and the whole information is in attributes. A particular node might have just one child or more with no anticipation.
How do I make sure that I have a 0th
child node even if there is just one child like below?
Required O/P:
[node1] => Array
(
[@attributes] => Array
(
[attr1] => val1
[attr2] => val2
)
[node2] => Array
(
[0] => Array
(
[@attributes] => Array
(
[attr3] => val3
[attr4] => val4
)
)
)
)
Note: A simple workaround I devised
if(isset($array['node1']['node2']['@attributes']){
print_r($array['node1']['node2']['@attributes']);
}
else{
foreach($array['node1']['node1'] AS $node){
print_r($node['@attributes']);
}
}
But the XML response is easily 7000 lines and more that 20 different nodes present in different levels. I dont want to use this condition explicitly for each node.
PS: I have looked into this question but I want the opposite. Add a 0th item even if there is one child PHP convert XML to JSON group when there is one child