很長時間無法解決smth看起來很簡單的問題...我想合併一個二維數組。 的例子:在PHP中合併二維數組
$arr1 = {
[532] =
{
[0] = "11"
[1] = "12"
}
[273] =
{
[0] = "99"
}
}
$arr2 = {
[532] =
{
[0] = "11"
[1] = "13"
}
}
和合並的結果應該是,在地圖上常見的按鍵,完全一樣的陣列:
$result = {
[532] =
{
[0] =
{
[0] = "11"
[1] = "12"
}
[1] =
{
[0] = "11"
[1] = "13"
}
}
[273]
[0] =
{
[0] = "99"
}
[1] =
{
}
}
我嘗試sometihng這樣的:
$result = $arr1;
foreach ($arr2 as $key => $value) {
$result[$key] = isset($result[$key]) ? array_merge([$result[$key]], [$value]) : [$value];
}
但它不工作,如果$ arr2是空的:(
你是什麼意思'如果$ arr2是空的'。如果你沒有第二個數組,爲什麼要合併? – jitendrapurohit