1

I have a multidimensional array as such:

$food= array
  (
  array("Rye"   =>1, "Wheat" =>4, "White" =>4 ),
  array("Apple" =>2, "Orange"=>1, "Banana"=>5 ), <---data I want
  array("Cheese"=>2, "Milk"  =>1, "Cream" =>5 )
);

Is there a way I can use a foreach loop to loop through the 2nd child array (fruit data)?

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
MeltingDog
  • 14,310
  • 43
  • 165
  • 295

6 Answers6

3

Like this?

foreach($food as $produce) {
    foreach($produce as $name => $value) {
        echo "The produce $name has value: $value\n";
    }

}

The first for loop just loops through the first array, it doesn't have significant keys we need, so I just get the reference to the value and store it in the value $produce

Then, we loop through the array that is $produce, but this time key and value are both significant.

That's why we use $name => $value in this loop, so we get both values we need.
Some prefer to always use $key => $value, but I prefer to give variables the name of the value they represent.

Now if you need a specific fruit, you could wrap it in a function to search for it

/**
 * Returns the fruit name from supplied food array.
 * @var $food array[array[string => value]]
 * @var $fruitName string The name of the fruit you want
 * @returns 
 **/
function findFruit($food,$fruitName) {
     foreach($food as $produce) {
        foreach($produce as $name => $value) {
            if($name == $fruitName) {
               return $value;
            }
        }
    }
} 
$quantityOfBananas = findFruit($food, "Banana");//5
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
0

You will have to use nested foreach,

foreach($food as $item){
    foreach($item as $key => $value){
        echo $key; //Rye
        echo $value; // 1
    }
}
Akshay Gohil
  • 370
  • 2
  • 10
0

To loop child array you have use two loops

<?php

    $food= array
      (
      array("Rye"=>1,"Wheat"=>4,"White"=>4),
      array("Apple"=>2,"Orange"=>1,"Banana"=>5), 
      array("Cheese"=>2,"Milk"=>1,"Cream"=>5)
    );

    foreach($food  as $data){
         foreach($data as $key=>$value){
           echo "key=".$key." value=".$value.'<br>';
    }
}

output will be

key=Rye value=1
key=Wheat value=4
key=White value=4
key=Apple value=2
key=Orange value=1
key=Banana value=5
key=Cheese value=2
key=Milk value=1
key=Cream value=5
Vision Coderz
  • 8,257
  • 5
  • 41
  • 57
0

Try this:

$food= array
(
  array("Rye"=>1,"Wheat"=>4,"White"=>4),
  array("Apple"=>2,"Orange"=>1,"Banana"=>5),    
  array("Cheese"=>2,"Milk"=>1,"Cream"=>5)
);

foreach($food as $key => $fruits){
  if($key == 1){
   foreach($fruits as $ke => $fruit){
    echo $ke."-".$fruit."<br>";
  }
}
}

PS: Your wheat has syntax error "Wheat"->4

Kinshuk Lahiri
  • 1,468
  • 10
  • 23
0

you can use foreach inside the foreach (nested foreach) like below

foreach($food as $f)
  {
    foreach ($f as $fn => $sn)
    {
        echo $fn.$sn;

    }
  }
Arun Kumaresh
  • 6,211
  • 6
  • 32
  • 50
0

Since you know it's always the 2nd child, I would keep it simple and do something like this:

$fruits = $food[1];
foreach($fruits as $key => $value) {
    // do something
}

There is no need for two foreach loops.

simon
  • 2,896
  • 1
  • 17
  • 22