This forum is now read-only. Please use our new forums! Go to forums
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[2][0] . "<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/>"
?>
Answer 557b65c79113cb0f140000f1
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[0]
. This is the row. If we want the first item in $myArrays[0]
, we need to pass $myArrays[0][0]
. This is the row AND column. $myArrays[0][0]
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
.
Answer 5571e8d976b8fe93cd0004a4
hey can you response me how I can make at 4/7 i will say how to figure out
Answer 5572629ae39efe24a5000719
Answer 557280969376769251000242
Answer 557281089113cbe8f40003c5
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 . ‘.’ . ‘
‘;
}
1 comments
Ok thanks, I’ll try it out! All the best!
Answer 557281509376765b0f00033f
say if it work and if its not i will send you the entire code
Answer 557281d69113cb3d910003a9
i think you have a bug the code is working
Answer 55d0a5a29113cba78d0002a5
I don’t understand this exercise. It asks you to create an ASSOCIATIVE array but expects you to display its values using an indexed for loop, i.e. for ($i = 0; $i < $length; $i++). The Hint section suggests using the count() function, which is flat out wrong.
You CANNOT access the contents of an associative array that way. $myArray[0] will throw an invalid offset error. You have to use a foreach construct. My code outputs the values of the array as instructed in the editor, but the sidebar instructions say to print out the entire contents. This is a glaring inconsistency. Hello, QA department! My code does print out the contents of the array but the editor incorrectly states that my code doesn’t print out all of the values in the array. This is really frustrating. Codeacademy needs to stop dropping the ball.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
i don’t understand what [$i++][$l] and so on means, could someone explain?