PHP

php arraymerge能处理多维数组吗

小樊
81
2024-12-09 23:08:24
栏目: 编程语言

是的,array_merge() 函数可以处理多维数组。它会按照从左到右的顺序合并一个或多个数组,包括多维数组。当合并多维数组时,它会递归地将数组的每个元素合并在一起。

下面是一个例子:

<?php
$array1 = array(
    "color" => "red",
    "shape" => "trapezoid",
    "prices" => array(
        "current" => 10,
        "previous" => 20
    )
);

$array2 = array(
    "color" => "blue",
    "shape" => "rectangle",
    "prices" => array(
        "current" => 15,
        "previous" => 25
    )
);

$result = array_merge($array1, $array2);
print_r($result);
?>

输出结果:

Array
(
    [color] => blue
    [shape] => rectangle
    [prices] => Array
        (
            [current] => 15
            [previous] => 25
        )
)

可以看到,array_merge() 函数成功地将两个多维数组合并为一个。

0
看了该问题的人还看了