#02:2次元配列を作成する
ここでは、PHPを使って、実際に2次元配列を作成してみましょう。例として、2次元配列を作成して、そこから要素を表示します。
2次元配列とは、2つのインデックスで要素を指定する配列のこと。例えば、
$arr[3][4] = 100;
とすると、インデックス 3, 4 に対応する要素100を代入できる。
<?php
// 2次元配列を作成する
$player = "忍者";
$teamA = [$player, "戦士", "魔法使い"];
echo($teamA[0] . ", ");
echo($teamA[1] . ", ");
echo($teamA[2] . "\n");
?>
<?php
// 2次元配列を作成する
$player = "忍者";
$teamA = [$player, "戦士", "魔法使い"];
echo($teamA[0] . ", ");
echo($teamA[1] . ", ");
echo($teamA[2] . "\n");
$teamB = [$teamA[0], $teamA[1], $teamA[2]];
echo($teamB[0] . ", ");
echo($teamB[1] . ", ");
echo($teamB[2] . "\n");
?>
<?php
// 2次元配列を作成する
$player = "忍者";
$teamA = [$player, "戦士", "魔法使い"];
echo($teamA[0] . ", ");
echo($teamA[1] . ", ");
echo($teamA[2] . "\n");
$teamB = [$teamA[0], $teamA[1], $teamA[2]];
echo($teamB[0] . ", ");
echo($teamB[1] . ", ");
echo($teamB[2] . "\n");
$teamC = ["勇者", "戦士", "魔法使い"];
$teamD = ["盗賊", "忍者", "商人"];
$teamE = ["スライム", "ドラゴン", "魔王"];
$teams = [$teamC, $teamD, $teamE];
echo($teams[0][0] . ", ");
echo($teams[0][1] . ", ");
echo($teams[0][2] . "\n");
?>
<?php
// 2次元配列を作成する
$player = "忍者";
$teamA = [$player, "戦士", "魔法使い"];
echo($teamA[0] . ", ");
echo($teamA[1] . ", ");
echo($teamA[2] . "\n");
$teamB = [$teamA[0], $teamA[1], $teamA[2]];
echo($teamB[0] . ", ");
echo($teamB[1] . ", ");
echo($teamB[2] . "\n");
$teamC = ["勇者", "戦士", "魔法使い"];
$teamD = ["盗賊", "忍者", "商人"];
$teamE = ["スライム", "ドラゴン", "魔王"];
$teams = [$teamC, $teamD, $teamE];
echo($teams[2][0] . ", ");
echo($teams[2][1] . ", ");
echo($teams[2][2] . "\n");
?>
» 【PHP】配列 - Qiita
https://qiita.com/tokutoku393/items/ea2a61765eda1dee8f2e
» PHPのArray(配列・連想配列)メソッド・使い方まとめ | 侍エンジニア塾ブログ | プログラミング入門者向け学習情報サイト
https://www.sejuku.net/blog/11981
» arrayとは - PHP用語 Weblio辞書
https://www.weblio.jp/content/array