超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<array_maparray_merge>
Last updated: Fri, 22 Jun 2007

array_merge_recursive

(PHP 4 >= 4.0.1, PHP 5)

array_merge_recursive -- 递归地合并两个或多个数组

说明

array array_merge_recursive ( array array1, array array2 [, array ...])

array_merge_recursive() 将两个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。

如果输入的数组中有相同的字符串键名,则这些值会被合并到一个数组中去,这将递归下去,因此如果一个值本身是一个数组,本函数将按照相应的条目把它合并为另一个数组。然而,如果数组具有相同的数组键名,后一个值将不会覆盖原来的值,而是附加到后面。

例子 1. array_merge_recursive() 例子

<?php
$ar1
= array ("color" => array ("favorite" => "red"), 5);
$ar2 = array (10, "color" => array ("favorite" => "green", "blue"));
$result = array_merge_recursive ($ar1, $ar2);
?>

$result 成为:

Array
(
   [color] => Array
       (
           [favorite] => Array
               (
                   [0] => red
                   [1] => green
               )

           [0] => blue
       )

   [0] => 5
   [1] => 10
)

参见 array_merge()




add a note add a note User Contributed Notes
array_merge_recursive
shemari75 at mixmail dot com
19-Dec-2003 01:22
Here's a function to recursively merge any number of any-dimensional arrays.
It actually works in quite a similar way as array_merge_recursive does, but with two major differences:
- Later elements overwrite previous ones having the same keys.
- Numeric keys are not appended. Instead, they are converted into associative ones, and therefore overwritten as stated above.

Usage:
   array array_merge_n(array array1, array array2[, array ...])

<?php
 /**
    *  Merges two arrays of any dimension
    *
    * This is the process' core!
    *  Here each array is merged with the current resulting one
    *
    *  @access private
    * @author Chema Barcala Calveiro <shemari75@mixmail.com>
    * @param array $array   Resulting array - passed by reference
    * @param array $array_i Array to be merged - passed by reference
    */

   function array_merge_2(&$array, &$array_i) {
     // For each element of the array (key => value):
       foreach ($array_i as $k => $v) {
           // If the value itself is an array, the process repeats recursively:
           if (is_array($v)) {
               if (!isset($array[$k])) {
                  $array[$k] = array();
               }
              array_merge_2($array[$k], $v);

           // Else, the value is assigned to the current element of the resulting array:
           } else {
               if (isset($array[$k]) && is_array($array[$k])) {
                   $array[$k][0] = $v;
             } else {
                   if (isset($array) && !is_array($array)) {
                       $temp = $array;
                      $array = array();
                       $array[0] = $temp;
                   }
                   $array[$k] = $v;
              }
           }
       }
   }

   /**
  *  Merges any number of arrays of any dimension
    *
    *  The arrays to be merged are passed as arguments to the function,
    * which uses an external function (array_merge_2) to merge each of them
   *  with the resulting one as it's being constructed
    *
    * @access public
    *  @author Chema Barcala Calveiro <shemari75@mixmail.com>
    *  @return array

<array_maparray_merge>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22