PHP中的array_merge_recursive()函數是一種將兩個或多個數字組合成一個數組的方法,它和array_merge()函數的不同之處在於它不僅僅只合併數組元素,而是將每個數組的元素遞歸組合成一個更大的數組。因此,array_merge_recursive()函數在深層巢狀、需要遞歸地合併陣列時非常有用。這篇文章將介紹array_merge_recursive()函數的具體使用方法。
array_merge_recursive()函數的基本語法如下:
array array_merge_recursive(array $array1 [, array $array2 [, array $... ]] )
##############################################################n其中,$array1是必要的參數,表示要合併的第一個陣列。 $array2到$arrayN是可選的參數,表示要合併的其他陣列。 ######實例1:######下面是一個簡單的例子,展示如何將兩個數字組合並為一個:###$array1 = array("color" => array("favorite" => "red"), 5); $array2 = array(10, "color" => array("favorite" => "green", "blue")); $result = array_merge_recursive($array1, $array2); print_r($result);
Array ( [color] => Array ( [favorite] => Array ( [0] => red [1] => green ) [0] => blue ) [0] => 5 [1] => 10 )
$people = array( "John" => array("age" => 25, "sex" => "male"), "Sarah" => array("age" => 31, "sex" => "female"), ); $people = array_merge_recursive($people, array( "John" => array("height" => "6'1""), "Michael" => array("age" => 37) )); print_r($people);
Array ( [John] => Array ( [age] => Array ( [0] => 25 ) [sex] => Array ( [0] => male ) [height] => Array ( [0] => 6'1" ) ) [Sarah] => Array ( [age] => Array ( [0] => 31 ) [sex] => Array ( [0] => female ) ) [Michael] => Array ( [age] => Array ( [0] => 37 ) ) )
以上是PHP array_merge_recursive()函數使用方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!