Iterating through a multidimensional array with foreach() : foreach « Statement « PHP






Iterating through a multidimensional array with foreach()

 
<?
$flavors = array('Japanese' => array('hot' => 'A',
                                     'salty' => 'B'),
                 'Chinese'  => array('hot' => 'D',
                                     'pepper-salty' => 'C'));

foreach ($flavors as $culture => $culture_flavors) {
    foreach ($culture_flavors as $flavor => $example) {
        print "A $culture $flavor flavor is $example.\n";
    }
}
?>
  
  








Related examples in the same category

1.Foreach Demo
2.Looping through an Enumerated Array
3.Array element order and foreach()
4.Modifying an array with foreach()
5.Using foreach() to Iterate Through an Array
6.Using foreach() with numeric arrays
7.Displaying the contents of an array using a loop
8.Iterating Through Object Properties
9.Implementing the traversable Iterator