A month ago I asked how to deserialize a XML in c#, now I have to do the same but in PHP, but I can't make it work correctly, This is my XML format:
<?xml version="1.0" encoding="UTF-8" ?>
<response uri="/api/" action="EXPORT">
<result>
<rows>
<row>
<column name="Name1">Value1</column>
<column name="Name2">Value2</column>
</row>
<row>
<column name="Name1">Value1</column>
<column name="Name2">Value2</column>
</row>
</rows>
</result>
</response>
And here is what I'm trying to do:
$row = new SimpleXMLElement($XML);
$json = json_encode($row);
$array = json_decode($json,TRUE);
foreach($array["result"]["rows"]["row"] as $row){
$array[$k] = [
'Name1' => $row["column"][0],
'Name2' => $row["column"][1]
];
$k++;
}
The problem with this is that if a use a foreach like:
foreach($array as $arr){
print_r($arr);
echo"<br>";
}
Gives me this result:
Array ( [uri] => /api/ [action] => EXPORT )
Array ( [rows] => Array ( [row] => Array ( [0] => Array ( [column] => Array ( [0] => Value1 [1] => Value2 ) ) [1] => Array ( [column] => Array ( [0] => Value1 [1] => Value2 ) ) ) ) )
Array ( [Name1] => Value1 [Name2] => Value2 )
Array ( [Name1] => Value1 [Name2] => Value2 )
And I just want the last 2 results it prints, because that was actually what I need, how can I fix this?