LoginSignup
45
46

More than 5 years have passed since last update.

[PHP]多次元連想配列の扱い

Last updated at Posted at 2014-06-08

本記事では、連想配列のインデックス配列を「多次元連想配列」と呼ぶことにする。

多次元連想配列に値を代入

$array[] = array('ID'=>7, 'title'=>'多次元配列の扱い');
$array[] = array('ID'=>6, 'title'=>'画像圧縮と軽量化のすすめ');
$array[] = array('ID'=>5, 'title'=>'初心者のためのgitコマンド');

多次元連想配列から値を取得

foreach ($array as $value) { //3回繰り返し
  echo $value['ID'];    // 7 → 6 → 5
  echo $value['title']; // 多次元配列の扱い →(略)
}

連想配列の連想配列から値を取得したい場合はforeach文のネスト。

foreach ($array as $key1 => $value1) {
  foreach ($value1 as $key2 => $value2) {
    print $value2 . ", "; //「.」は文字列連結
  }
}

参照URL:http://web-dou.com/php/array2.html

45
46
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
45
46