[PHP7.4.11] 二次元配列

配列の中に配列

$ary = [
["a", "b", "c", "d"],
["e", "f", "g", "h"],
["i", "j", "k", "l"]
];

echo "<pre>";
var_dump($ary);
echo "</pre>";

配列の追加

$ary = [
// 省略
];

$ary[] = ["m", "n", "o", "p"];

多次元配列

$ary = [
	"fruits" => ["apple", "banana"],
	"animal" => ["monkey", "cat"]
];

多次元配列の追加

$ary = [
	"fruits" => ["apple", "banana"],
	"animal" => ["monkey", "cat"]
];

$ary_b = [
	"contory" => ["japan","usa"]
];


echo "<pre>";
var_dump(array_merge($ary, $ary_b));
echo "</pre>";

ただこれ、下のようにやると、fruitsが上書きされてしまうな。

$ary = [
	"fruits" => ["apple", "banana"],
	"animal" => ["monkey", "cat"]
];

$ary_b = [
	"fruits" => ["kui"]
];

こうすると上手くいく

$ary = [
	"fruits" => ["apple", "banana"],
	"animal" => ["monkey", "cat"]
];

$ary["fruits"][] = "kui";

echo "<pre>";
var_dump($ary);
echo "</pre>";

OK, これを応用して考える