foreach文

  • 2022/04/30
  • PHP

$my_array = array(“item1”, “item2”, “item3”, “item4”, “item5”);
// 配列の要素を出力

foreach($my_array as $value){ 
  echo $value . “\n”;
}


連想配列の場合

$my_array = array(
  ”key1″ => “value1”,
  ”key2″ => “value2”,
  ”key3″ => “value3”,
  ”key4″ => “value4”,
  ”key5″ => “value5”
);

// 連想配列の要素を出力
foreach($my_array as $key => $value){ 
   echo “key:” . $key . “, value:” . $value . “\n”;


// valueのみを取得
foreach($my_array as $value){ 
   echo “value:” . $value . “\n”;
}

参考URL : https://www.fenet.jp/dotnet/column/language/6290/