From a web service I am reading an xml file with the following structure
<ArrayOfStoreDetails xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
<StoreDetails>
<ItemId>138253</ItemId>
<ItemPrice1>9.68</ItemPrice1>
</StoreDetails>
<StoreDetails>
<ItemId>159733</ItemId>
<ItemPrice1>35.87</ItemPrice1>
</StoreDetails>
</ArrayOfStoreDetails>
I then use the following php code to parse through the data
$response = curl_exec($curl);
$products = new SimpleXMLElement($response);
// convert to assoc array
$objJsonDocument = json_encode($products);
$productsarray = json_decode($objJsonDocument, TRUE);
foreach ($productsarray['StoreDetails'] as $key => $value) {
echo $key . " => " . $value . "<br>";
}
I would expect the code above to display
ItemId => 138253 ItemPrice => 9.68 ItemId => 159733 ItemPrice => 35.87
Instead my output is
0 => Array 1 => Array
I am real beginner in php and cant really figure this out… Any help please?
Advertisement
Answer
$productsArray['StoreDetails'] is a 2-dimensional array.
$value is an associative array with ItemID and ItemPrice1 elements. You need to print those.
foreach ($productsarray['StoreDetails'] as $value) {
echo $value['ItemID'] . " => " . $value['ItemPrice1'] . "<br>";
}