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 3 years have passed since last update.

連想配列で使うforeach

Posted at

連想配列を利用することで繰り返し表示することができる。


 'りんご',
    'grape' => 'ぶどう',
    'lemon' => 'レモン',
    'tomato' => 'トマト',
    'peach' => 'もも'
];

$fruits = [
    'apple' => 'りんご',
    'grape' => 'ぶどう',
    'lemon' => 'レモン',
    'tomato' => 'トマト',
    'peach' => 'もも'
];
//りんご、ぶどうetcの右側の日本語だけを取り出す
foreach($fruits as $val){
    print($val . "\n");
}

//インデックスごと取り出すときは
foreach($fruits as $english => $japanese){
    print($english .  ':' . $japanese . "\n");
}
//"$english"と"$japanese"の部分は、様々な値に変更することが可能。
//例えば、"$val"や"item_name"など変更するものによる
?>
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?