0 points Submitted by GGH42
over 6 years

# 7/7 How to iterate through a multidimensional associative array?

This is my solution for treating a multidimensional array, but I’m not too sure how to do something similar for a multidimensional associative array. Could somebody give me a little help with how to achieve this. Cheers! :)

``````<?php
// On the line below, create your own associative array:
\$myArray=array(array('Ball','blue'),
array('Triangle','green'),
array('Cube', 'red'));

// On the line below, output one of the values to the page:
echo \$myArray . "<br/><br/>";

// On the line below, loop through the array and output
// *all* of the values to the page:
for(\$i=0; \$i < count(\$myArray); \$i++)
{
for (\$j=0; \$j < count(\$myArray[\$i]); \$j++)
{
echo \$myArray[\$i][\$j] . "\n";
}
echo ". <br/>";
}
?>``````

HOW WOULD I ITERATE THROUGH THIS FOR EXAMPLE:

``````    <?php
// On the line below, create your own associative array:
\$myArray=array(array('Ball'=>'blue','Triangle'=>'green'),
array('Cube'=> 'red','Cylinder'=>'pink'));

// On the line below, output one of the values to the page:
foreach(\$myArray as \$key=>\$value)
{
echo \$value . " ". \$value. "<br/><br/>";
}
// On the line below, loop through the array and output

echo "<br/>"

?>``````

1 vote

Think of multidimensional arrays as tables. In a table you need to get the row and column of an item you want.

To get any value in an array that is nested within another array, we first need to get to the array we want. This will be the row. Then we need to access the item within the array that we want. This will be the column. Think of the formula to access an item in a multidimensional array as:

`\$arrayOfArrays[row][column]`.

In the below example, if we want the first array in `\$myArrays`, we need to pass `\$myArrays`. This is the row. If we want the first item in `\$myArrays`, we need to pass `\$myArrays`. This is the row AND column. `\$myArrays` will retrieve the first item in the first array. Below I use nested for loops to iterate through every item in every array that is within `\$myArrays` and `print` it:

``````\$myArrays = array(
array(1, 2, 3, 4),
array(5, 6, 7, 8),
array(9, 10, 11, 12),
array(13, 14, 15, 16)
);

for (\$i = 0; \$i < count(\$myArrays); \$i++) {
for (\$l = 0; \$l < count(\$myArrays[\$i]); \$l++) {
print \$myArrays[\$i][\$l];
print "<br/>";
};
};``````

The above code will print each item in each array in `\$myArrays`, which looks like this:

``````1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16``````

You can substitute `print` for `echo`.

points Submitted by George Perez
over 6 years lars.berg about 6 years

i don’t understand what [\$i++][\$l] and so on means, could someone explain?

hey can you response me how I can make at 4/7 i will say how to figure out

points Submitted by Arian Atapour
over 6 years GGH42 over 6 years

Which one is that? GGH42 over 6 years

Oh, I think I know the one you’re talking about!:

Arrays and Maps: (4/7)

points Submitted by GGH42
over 6 years

points Submitted by Arian Atapour
over 6 years

Try this // On the line below, output one of the values to the page: echo \$myArray[‘name’] . “
“; echo \$myArray[‘age’] . ‘
‘;

// On the line below, loop through the array and output // all of the values to the page: foreach (\$myArray as \$a=>\$b){ print ‘my ‘ . \$a . ‘ is ‘ . \$b . ‘.’ . ‘
‘; }

points Submitted by Arian Atapour
over 6 years GGH42 over 6 years

Ok thanks, I’ll try it out! All the best!

say if it work and if its not i will send you the entire code

points Submitted by Arian Atapour
over 6 years

i think you have a bug the code is working

points Submitted by Arian Atapour
over 6 years 