Array merge recursive

There are a lot of examples here for recursion that are meant to behave more like array_merge() but they don't get it quite right or are fairly customised. I think this version is most similar, takes more than 2 arguments and can be renamed in one place:

<?php

function array_merge_recursive_simple() {

    if (func_num_args() < 2) {
       
trigger_error(__FUNCTION__ .' needs two or more array arguments', E_USER_WARNING= array_shift($arrays);
        if (!
is_array($array)) {
           
trigger_error(__FUNCTION__ .' encountered a non array argument', E_USER_WARNING);
            return;
        }
        if (!
$array)
            continue;
        foreach (

rd">($value) && array_key_exists($key, $merged) && is_array($merged[$key]))
                   
$merged[$key] = call_user_func(__FUNCTION__, $merged[$key],
$merged[] = $value;
    }
    return
$merged;
}

$a1 = array(
   
88 => 1,
   
'foo' => 2,
   
'bar' => array(4),
   
'x' => 5,
   
'z'
ult">7,
   
'foo' => array(8),
   
'bar' => 9,
   
'y' => 10,
   
'z' => array(
       
'm' => 'bye',
       
11,
    ),
);
$a3 = array(
   
'z' <.
">,
$a3));
var_dump(array_merge_recursive_simple($a1, $a2, $a3));
var_dump(array_merge_recursive($a1, $a2, $a3));
?>

gives:

array(7) {              array(7) {              array(7) {
  int(1)     .
p;    [0]=>                   [0]=>
    int(8)                  int(8)                  int(2)
  }                       }                         [1]=>
  ["bar"]=>               ["bar"]=>                 int(8)
  int(9)                  int(9)                  }
  ["x"]=>                 ["x"]=>                 ["bar"]=>
  int(5)          &nbs.
bsp;  [0]=>                   [1]=>
    string(4) "ciao"        int(6)                  int(9)
  }                         ["m"]=>               }
  [1]=>                     string(4) "ciao"      ["x"]=>
  int(7)                    [1]=>                 int(5)
  ["y"]=>                   int(11)               ["z"]=>
  int(10)                 }          &nbs.
bsp;     ["y"]=>                   ["m"]=>
                          int(10)                   array(3) {
                        }                             [0]=>
                                                      string(2) "hi"
                                                      [1]=>
                        &nbs.
    string(4) "ciao"
                                                    }
                                                    [1]=>
                                                    int(11)
                                                  }
                                                  [1]=>
   .
bsp;           int(10)
                                                }

php.net

The array_merge_recursive() is an inbuilt function in PHP and is used to merge two or more arrays into a single array recursively. This function is used to merge the elements or values of two or more arrays together into a single array. The merging occurs in such a manner that the values of one array are appended at the end of the previous array. If there are same keys in the given arrays, then the key is assigned a value which has an array that consists of the values with the same key.

Note: The function is different from array_merge() in a way that in case of multiple arrays with same keys, array_merge() function takes the last array value from all the arrays, but in array_merge_recursive() the key is assigned an array which consists of all the values of arrays which has the same key.

Syntax:

array_merge_recursive($array1, $array2, $array3...$arrayn)

Parameters: The function can take any number of arrays as parameter separated by comma (,) that we need to merge. The first parameter $array1 is mandatory.

Return Value: The function returns a merged array which has all the arrays merged. The merging occurs in such a manner that the values of one array are appended at the end of the previous array. If there are same keys in the given arrays, then that key in the output array is assigned an array that consists of the values with the same key.

Examples:

  Input : $a1=array("a"=>"raj", "b"=>"striver");   $a2=array("z"=>"geeks", "b"=>"articles");  Output :   Array  (   [a] => raj   [b] => Array   (   [0] => striver   [1] => articles   )     [z] => geeks  )  Explanation: "striver" and "articles" has the same   key "b", so the key b is assigned to an array which has   "striver" and "articles" as elements.     Input :$a1=array("a"=>"raj", "b"=>"striver");   $a2=array("z"=>"geeks", "d"=>"articles");  Output :  Array  (   [a] => raj   [b] => striver   [z] => geeks   [d] => articles  )  

Below programs illustrate the array_merge_recursive() function:

Program 1: PHP program to demonstrate array_merge_recursive()
function with all different keys.

Output:

Array  (   [a] => raj   [b] => striver   [z] => geeks   [d] => articles  )  

Program 2: PHP program to demonstrate array_merge_recursive() function with same keys.

Output:

Array  (   [a] => raj   [b] => Array   (   [0] => striver   [1] => articles   )     [z] => geeks  )  

Reference:
http://php.net/manual/en/function.array-merge-recursive.php


www.geeksforgeeks.org

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 Resulting array, once all have been merged
     */

   function array_merge_n() {
      
// Initialization of the resulting array:
      
$array = array();

       // Arrays to be merged (function's arguments):
      
$arrays =& func_get_args();

       // Merging of each array with the resulting one:
      
foreach ($arrays as $array_i) {
           if (
is_array($array_i)) {
              
array_merge_2($array, $array_i);
           }
       }

       return $array;
   }
?>

php.svchat.ru

There are a lot of examples here for recursion that are meant to behave more like array_merge() but they don't get it quite right or are fairly customised. I think this version is most similar, takes more than 2 arguments and can be renamed in one place:

<?php

function array_merge_recursive_simple() {

    if (func_num_args() < 2) {
       
trigger_error(__FUNCTION__ .' needs two or more array arguments', E_USER_WARNING);
        return;
    }
   
$arrays = func_get_args();
   
$merged = array();
    while (
$arrays) {
       
$array = array_shift($arrays);
        if (!
is_array($array)) {
           
trigger_error(__FUNCTION__ .' encountered a non array argument', E_USER_WARNING);
            return;
        }
        if (!
$array)
            continue;
        foreach (
$array as $key => $value)
            if (
is_string($key))
                if (
is_array($value) && array_key_exists($key, $merged) && is_array($merged[$key]))
                   
$merged[$key] = call_user_func(__FUNCTION__, $merged[$key], $value);
                else
                   
$merged[$key] = $value;
            else
               
$merged[] = $value;
    }
    return
$merged;
}

$a1 = array(
   
88 => 1,
   
'foo' => 2,
   
'bar' => array(4),
   
'x' => 5,
   
'z' => array(
       
6,
       
'm' => 'hi',
    ),
);
$a2 = array(
   
99 => 7,
   
'foo' => array(8),
   
'bar' => 9,
   
'y' => 10,
   
'z' => array(
       
'm' => 'bye',
       
11,
    ),
);
$a3 = array(
   
'z' => array(
       
'm' => 'ciao',
    ),
);
var_dump(array_merge($a1, $a2, $a3));
var_dump(array_merge_recursive_simple($a1, $a2, $a3));
var_dump(array_merge_recursive($a1, $a2, $a3));
?>

gives:

array(7) {              array(7) {              array(7) {
  int(1)                  int(1)                  int(1)
  ["foo"]=>               ["foo"]=>               ["foo"]=>
  array(1) {              array(1) {              array(2) {
    [0]=>                   [0]=>                   [0]=>
    int(8)                  int(8)                  int(2)
  }                       }                         [1]=>
  ["bar"]=>               ["bar"]=>                 int(8)
  int(9)                  int(9)                  }
  ["x"]=>                 ["x"]=>                 ["bar"]=>
  int(5)                  int(5)                  array(2) {
  ["z"]=>                 ["z"]=>                   [0]=>
  array(1) {              array(3) {                int(4)
    ["m"]=>                 [0]=>                   [1]=>
    string(4) "ciao"        int(6)                  int(9)
  }                         ["m"]=>               }
  [1]=>                     string(4) "ciao"      ["x"]=>
  int(7)                    [1]=>                 int(5)
  ["y"]=>                   int(11)               ["z"]=>
  int(10)                 }                       array(3) {
}                         [1]=>                     [0]=>
                          int(7)                    int(6)
                          ["y"]=>                   ["m"]=>
                          int(10)                   array(3) {
                        }                             [0]=>
                                                      string(2) "hi"
                                                      [1]=>
                                                      string(3) "bye"
                                                      [2]=>
                                                      string(4) "ciao"
                                                    }
                                                    [1]=>
                                                    int(11)
                                                  }
                                                  [1]=>
                                                  int(7)
                                                  ["y"]=>
                                                  int(10)
                                                }

php.theraven7.com

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 Resulting array, once all have been merged
     */

    function array_merge_n() {
       
// Initialization of the resulting array:
       
$array = array();

        // Arrays to be merged (function's arguments):
       
$arrays =& func_get_args();

        // Merging of each array with the resulting one:
       
foreach ($arrays as $array_i) {
            if (
is_array($array_i)) {
               
array_merge_2($array, $array_i);
            }
        }

        return $array;
    }
?>

php.segmenta.ru


You May Also Like

About the Author: admind

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *

Этот сайт использует Akismet для борьбы со спамом. Узнайте как обрабатываются ваши данные комментариев.

Adblock
detector