This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by GGH42
almost 9 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[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

1 vote

Permalink

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.

points
Submitted by George Perez
almost 9 years

1 comments

lars.berg over 8 years

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

Answer 5571e8d976b8fe93cd0004a4

0 votes

Permalink

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

points
Submitted by Arian Atapour
almost 9 years

2 comments

GGH42 almost 9 years

Which one is that?

GGH42 almost 9 years

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

Answer 5572629ae39efe24a5000719

0 votes

Permalink

Arrays and Maps: (4/7)

points
Submitted by GGH42
almost 9 years

Answer 557280969376769251000242

0 votes

Permalink

ok i will answer now

points
Submitted by Arian Atapour
almost 9 years

Answer 557281089113cbe8f40003c5

0 votes

Permalink

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
almost 9 years

1 comments

GGH42 almost 9 years

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

Answer 557281509376765b0f00033f

0 votes

Permalink

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

points
Submitted by Arian Atapour
almost 9 years

Answer 557281d69113cb3d910003a9

0 votes

Permalink

i think you have a bug the code is working

points
Submitted by Arian Atapour
almost 9 years

Answer 55d0a5a29113cba78d0002a5

0 votes

Permalink

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.

points
Submitted by Urso Bearfriend
over 8 years