Reputation: 688
So here is portion of the array that i am trying to work with, it is pulled in from an xml file.
Array
(
[0] => Array
(
[SKU] => 0016
[StandardProductID] => 32109876453210
[Condition] => NEW
[ItemPackageQuantity] => 1
[Currency] => GBP
[StandardPrice] => 5.00
[DescriptionData] => Array
(
[Title] => Product Title
[Brand] => Franks
[Ingredients] => Array
(
[Ingredient] => Array
(
[0] => Water (Aqua)
[1] => Dicetyldimonium Chloride
)
)
I have worked out that I can access the first column of the array with this: $Details = std_class_object_to_array($xml);
foreach ($Details[product] as $Detail) {
if (strtoupper(trim(!empty($Detail[SKU])))) {
$SKU = (strtoupper(trim($Detail[SKU])));
echo $SKU;
}
}
But how do I go about accessing the other columns, DescriptionData/Title and DescriptionData/Ingredients/Ingredient. Can someone put together or point me in the right direction for working with the different levels in the array?
Many thanks in advance
Upvotes: 0
Views: 140
Reputation: 4505
In PHP arrays are really more like "maps" or "associative arrays" depending on what term you want to use. They can also follow the rules of a typical numerical indexed simple array as well, which makes it even more confusing when it's mixed and matched, but that's not the issue at hand.
When accessing an array remember that each element can contain its own array. When you use the [] operator you get the element at that location and then can work directly after that to call member functions or further access nested array elements.
This means $Details['DescriptionData'] would return an array, and because arrays can access elements with [] we can then chain another access after that: $Details['DescriptionData']['Ingredients'] which again returns an array so we can further chain $Details['DescriptionData']['Ingredients']['Ingredient'] and that's another array so we can chain one more time:
$Details['DescriptionData']['Ingredients']['Ingredient'][0] which returns a string "Water (Aqua)"
Strings can also access individual characters with the [] operator, so we could get the second character in Water by doing:
$Details['DescriptionData']['Ingredients']['Ingredient'][0][1] === 'a'
And if we were accessing an object inside an array you can do something like $Array['Index']->memberFunction(); If memberFunction returned an array you can end up with stuff like:
$Array['Index']->memberFunction()['ReturnedArrayIndex']; Because each access works on the returned value of the thing to the left of it.
It should be noted that large chains like this are brittle because at any point if something stops existing where you expect it to you'll end up with an invalid expression. So it's best to avoid writing code like the above even if it's important in understanding how access works.
Upvotes: 0
Reputation: 1553
I believe your foreach
loop is setup wrong. Assuming $Details
is defined as the array you have shown above:
foreach ($Details as $Detail) {
echo $Detail['SKU'];
echo $Detail['DescriptionData'];
echo $Detail['DescriptionData']['Title'];
}
Upvotes: 1
Reputation: 546055
First point - array keys should be addressed as a string:
// good:
$Details['product']
// bad:
$Details[product]
Now to your question:
Nested arrays are addressed just by using more square brackets:
echo $Detail['DescriptionData']['Ingredients']['Ingredient'][0]; // "Water (Aqua)"
Upvotes: 5
Reputation: 3088
echo $Detail['DescriptionData']['Title'];
foreach ($Detail['DescriptionData']['Ingredients']['Ingredient'] as $ingredient) {
echo $ingredient;
}
Upvotes: 0
Reputation: 13972
$title = $Detail['DescriptionData']['Title'];
$brand = $Detail['DescriptionData']['Brand'];
$ingredients = $Detail['DescriptionData']['Ingredients']['Ingredient']; //sets it to the array
//etc...
Upvotes: 3