1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

php Using Multidimensional Arrays

Posted at
qiita.rb
//2018.0731
//Using Multidimensional Arrays

$meals = array('breakfast' => ['Walnut Bun','Coffee'],
               'lunch'     => ['Cashew Nuts','White Mushrooms'],
               'snack'     => ['Dried Mulberries','Salted Sesame Crab']);

$lunches = [['Chicken','Eggplant','Rice'],
            ['Beef','Scallions','Noodles'],
            ['Eggplant','Tofu']];

$flavors = array('Japanes' => array('hot' => 'wasabi',
                                    'salty' => 'soy sauce'),
                 'Chinese' => array('hot' => 'mustard',
                                    'papper-salty' => 'prickly ash'));

print $meals['lunch'][1];
print $lunches[0][1];
print $flavors['Japanes']['hot'];

$prices['dinner']['Sweet Corn and Asparagus'] = 12.50;
$prices['lunch']['Cashew Nuts and White Mushrooms'] = 4.95;
$prices['dinner']['Braised Bamboo Fungus'] = 8.95;

$prices['dinner']['total'] = $prices['dinner']['Sweet Corn and Asparagus'] +
                             $prices['dinner']['Braised Bamboo Fungus'];

$specials[0][0] = 'Chestnut Bun';
$specials[0][1] = 'Walnut Bun';
$specials[0][2] = 'Peanut Bun';
$specials[1][0] = 'Chestnut Salad';
$specials[1][1] = 'Walnut Salad';
// Leaving out the index adds it to the end of the array
// This creates $specials[1][2]
$specials[1][] = 'Peanut Salad';

$flavors = array('Japanese' => array('hot' => 'wasabi',
                                     'salty' => 'soy sauce'),
                'Chinese'   => array('hot' => 'mustard',
                                     'pepper-salty' => 'prickly ash'));

foreach ($flavors as $curture => $curture_flavor) {
  foreach ($curture_flavor as $flavor => $example) {
    print "A $curture $flavor flavor is $example.<br>";
  }
}
$specials = array(array('Chestnut Bun', 'Walnut Bun', 'Peanut Bun'),
                  array('Chestnut Salad','Walnut Salad', 'Peanut Salad'));

for ($i=0, $num_spacials = count($specials); $i < $num_spacials ; $i++) { 
  for ($m=0, $num_sub =count($specials[$i]); $m < $num_sub; $m++) { 
    print "<br><br>Element [$i][$m] is " . $specials[$i][$m] . "\n";
  }
}
qiita.rb
Element [0][0] is Chestnut Bun 

Element [0][1] is Walnut Bun 

Element [0][2] is Peanut Bun 

Element [1][0] is Chestnut Salad 

Element [1][1] is Walnut Salad 

Element [1][2] is Peanut Salad
qiita.rb
$cities = array('New York'     => array('NY' => '8175133'),
                'Los Angeles'  => array('CA' => '3792621'),
                'Chicago'      => array('IL' => '2695598'));

$cities_a = array('New York'    => ['NY','8175133'],
                'Los Angeles' => ['CA','3792621'],
                'Chicago'     => ['IL','2695598']);

foreach ($cities as $city => $place) {
 foreach ($place as $city_abb => $people) {
      print "$city '의 약자는' $city_abb 이고 인구는 $people 입니다.<br>";
  } 
}

print $cities_a['New York'][0];
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?