I am using PHP DOM XPath to read XML. XML is having following structure where I got stuck
<details>
<name>name1</name>
<address>address1</address>
</details>
<details>
<name>name2</name>
<mobile>mobileNum</mobile>
<address>address2</address>
</details>
I have array like
array(
'name',
'mobile',
'address'
);
I use this array and read values from XML and store that values in other array but in the first record mobile number is not present so it read mobile no from second record and inserts it in first record.
Expected output is
array
(
[0] => array(
'person_name' = name1,
'address' = address1
)
[1] => array(
'person_name' = name1,
'mobile_no' = mobileNum
'address' = address1
)
)
But I get output as
array
(
[0] => array(
'person_name' = name1,
'mobile_no' = mobileNum
'address' = address1
)
[1] => array(
'person_name' = name1,
'address' = address1
)
)
How can I differentiate values between two nodes with same name.
Code to read XML is
$nodes = array
(
'person_name' => 'name',
'mobile_no' => 'mobile',
'address' => 'address'
)
$final_data = array();
$node_values = '';
foreach($nodes as $key => $data)
{
$node_values = $xml->xpath('//details'.$data);
$node_values = json_decode(json_encode((array)$node_values), TRUE);
if(!empty($node_values))
{
$i = 0;
foreach($node_values as $d)
{
$final_data[$i][$key] = trim($d[0]);
$i++;
}
}
}