Quantcast
Channel: 配列タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 834

PHPでforeachを使って配列を操作してみましょう!

$
0
0
foreach 配列の要素を、foreachを使って1つ1つ取り出してみます。 index.php <?php $scores = [ '1番目' => 1, '2番目' => 2, '3番目' => 3, ]; foreach ($scores as $score) { echo $score . PHP_EOL; } 以下をターミナルに入力します。 ~$ php index.php 1つずつ出力されました。 ~ $ php main.php 1 2 3 ~ $ キーも一緒に取り出してみます。 $key => 追加します。 index.php <?php $scores = [ '1番目' => 1, '2番目' => 2, '3番目' => 3, ]; foreach ($scores as $key => $score) { echo $key . ' は ' . $score . PHP_EOL; //文字列で繋げて表示させます。 } ターミナルで以下を実行します。 ~$ php index.php キーも出力されました ☆ ~ $ php index.php 1番目 は 1 2番目 は 2 3番目 は 3 ~ $

Viewing all articles
Browse latest Browse all 834

Trending Articles