array_replace_recursive

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

array_replace_recursive渡された配列の要素を再帰的に置き換える

説明

array_replace_recursive(array $array, array ...$replacements): array

array_replace_recursive() は、 array の値をそれ以降の配列の同じ要素の値で置き換えます。 最初の配列のキーと同じキーが 2 番目の配列にあれば、 2 番目の配列の値が最初の配列の値を上書きします。 2 番目の配列に存在するキーが最初の配列に存在しなければ、 そのキーが新たに最初の配列内に作られます。 最初の配列にしか存在しないキーについては何も変わりません。 置き換え用の配列が複数渡された場合は渡した順に処理を行い、 後から渡した配列の値のほうが優先されます。

array_replace_recursive() は再帰的な処理を行います。 配列を再帰的にたどり、内部の値に対しても同じ手順で処理します。

最初の配列の値がスカラーの場合は、 2 番目の配列の値がスカラーであるか配列であるかにかかわらずそれで置き換えられます。 最初の配列の値と 2番目の配列 の値が両方配列である場合は、array_replace_recursive() が対応する値を再帰的に処理します。

パラメータ

array

要素を置き換えたい配列。

replacements

置き換えたい要素を含む配列。

返り値

配列を返します。エラーが発生した場合は null を返します。

例1 array_replace_recursive() の例

<?php
$base 
= array('citrus' => array( "orange") , 'berries' => array("blackberry""raspberry"), );
$replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry'));

$basket array_replace_recursive($base$replacements);
print_r($basket);

$basket array_replace($base$replacements);
print_r($basket);
?>

上の例の出力は以下となります。

Array
(
    [citrus] => Array
        (
            [0] => pineapple
        )

    [berries] => Array
        (
            [0] => blueberry
            [1] => raspberry
        )

)
Array
(
    [citrus] => Array
        (
            [0] => pineapple
        )

    [berries] => Array
        (
            [0] => blueberry
        )

)

例2 array_replace_recursive() での再帰的な挙動の例

<?php
$base 
= array('citrus' => array("orange") , 'berries' => array("blackberry""raspberry"), 'others' => 'banana' );
$replacements = array('citrus' => 'pineapple''berries' => array('blueberry'), 'others' => array('litchis'));
$replacements2 = array('citrus' => array('pineapple'), 'berries' => array('blueberry'), 'others' => 'litchis');

$basket array_replace_recursive($base$replacements$replacements2);
print_r($basket);

?>

上の例の出力は以下となります。

Array
(
    [citrus] => Array
        (
            [0] => pineapple
        )

    [berries] => Array
        (
            [0] => blueberry
            [1] => raspberry
        )

    [others] => litchis
)

参考

add a note add a note

User Contributed Notes 7 notes

up
11
Gregor at der-meyer dot de
14 years ago
Nice that this function finally found its was to the PHP core! If you want to use it also with older PHP versions before 5.3.0, you can define it this way:

<?php
if (!function_exists('array_replace_recursive'))
{
  function
array_replace_recursive($array, $array1)
  {
    function
recurse($array, $array1)
    {
      foreach (
$array1 as $key => $value)
      {
       
// create new key in $array, if it is empty or not an array
       
if (!isset($array[$key]) || (isset($array[$key]) && !is_array($array[$key])))
        {
         
$array[$key] = array();
        }
 
       
// overwrite the value in the base array
       
if (is_array($value))
        {
         
$value = recurse($array[$key], $value);
        }
       
$array[$key] = $value;
      }
      return
$array;
    }
 
   
// handle the arguments, merge one by one
   
$args = func_get_args();
   
$array = $args[0];
    if (!
is_array($array))
    {
      return
$array;
    }
    for (
$i = 1; $i < count($args); $i++)
    {
      if (
is_array($args[$i]))
      {
       
$array = recurse($array, $args[$i]);
      }
    }
    return
$array;
  }
}
?>

I called this function array_merge_recursive_overwrite() in my older projects, but array_replace_recursive() sounds quite better while they do the same.

If you implemented such a compatible function before and don't want to refactor all your code, you can update it with the following snippet to use the native (and hopefully faster) implementation of PHP 5.3.0, if available. Just start your function with these lines:

<?php
 
// as of PHP 5.3.0 array_replace_recursive() does the work for us
 
if (function_exists('array_replace_recursive'))
  {
    return
call_user_func_array('array_replace_recursive', func_get_args());
  }
?>
up
4
Pau Prat Torrella
4 years ago
Note that function will NOT replace a sub-tree from you $array1 if its value in $array2 is an empty array.
Even tho the key for this dimension is technically 'set'.

(I suppose it treats it as just another recursive level to dive in, finding no key to compare, backtracking while leaving this sub-tree alone)

For example:

$array1  = ['first' => ['second' => 'hello']];
$array2  = ['first' => []];             
$result = array_replace_recursive($array1, $array2);

$result is still: ['first' => ['second' => 'hello']].
up
0
David Spector
3 years ago
It seemed to me that the first argument is passed by reference, so it will contain the results of this operation, but experiment shows that this is not true. The array value from the first argument is modified by each succeeding argument, and the result is returned to the caller. The array passed as the first argument is not modified.
up
0
Uther
5 years ago
Note that the use of the word "replace" in the function's description is a bit misleading... as none of the parameters are passed by reference, it does not actually replace anything... this simply describes from which parameter the resulting values come.
up
0
msahagian at dotink dot org
11 years ago
This is a fairly concise version which does not rely on traditional recursion:

<?php
   
function array_replace_recursive($base, $replacements)
    {
        foreach (
array_slice(func_get_args(), 1) as $replacements) {
           
$bref_stack = array(&$base);
           
$head_stack = array($replacements);

            do {
               
end($bref_stack);

               
$bref = &$bref_stack[key($bref_stack)];
               
$head = array_pop($head_stack);

                unset(
$bref_stack[key($bref_stack)]);

                foreach (
array_keys($head) as $key) {
                    if (isset(
$key, $bref) && is_array($bref[$key]) && is_array($head[$key])) {
                       
$bref_stack[] = &$bref[$key];
                       
$head_stack[] = $head[$key];
                    } else {
                       
$bref[$key] = $head[$key];
                    }
                }
            } while(
count($head_stack));
        }

        return
$base;
    }
?>
up
-2
kyle [dot] florence [@t] gmail [dot] com
14 years ago
This might help out people who don't have 5.3 running:

<?php

// Joins two or more arrays together recursively; key/value pairs of the first
// array are replaced with key/value pairs from the subsequent arrays.  Any
// key/value pair not present in the first array is added to the final array
function array_join()
{
   
// Get array arguments
   
$arrays = func_get_args();

   
// Define the original array
   
$original = array_shift($arrays);

   
// Loop through arrays
   
foreach ($arrays as $array)
    {
       
// Loop through array key/value pairs
       
foreach ($array as $key => $value)
        {
           
// Value is an array
           
if (is_array($value))
            {
               
// Traverse the array; replace or add result to original array
               
$original[$key] = array_join($original[$key], $array[$key]);
            }

           
// Value is not an array
           
else
            {
               
// Replace or add current value to original array
               
$original[$key] = $value;
            }
        }
    }

   
// Return the joined array
   
return $original;
}

?>
up
-5
oliver dot coleman at gmail dot com
9 years ago
If you came here looking for a function to recursively find and replace (scalar) values in an array (also recurses through objects):

<?php
define
('RECURSIVE_REPLACE_MARKER', 'recursive_replace_r47yr74yr7623t74r3645236rtvghdcbnsgxbt67g5e21873891');
/**
* Recursively replaces scalar values in objects and arrays. The passed array or object is altered in place.
* @param mixed $data An object or array.
* @param mixed $find The scalar value to find.
* @param mixed $replace The value to replace with (need not be a scalar).
*/
function recursive_replace(&$data, $find, $replace){
  if(
is_array($data)) {
    if (!isset(
$data[RECURSIVE_REPLACE_MARKER])) {
     
$data[RECURSIVE_REPLACE_MARKER] = TRUE;
      foreach(
$data as $key=>$val) {
        if(
is_array($data[$key]) || is_object($data[$key])) {
         
recursive_replace($data[$key], $find, $replace);
        }
        else{
          if(
$data[$key] === $find) {
           
$data[$key] = $replace;
          }
        }
      }
      unset(
$data[RECURSIVE_REPLACE_MARKER]);
    }
  }
  elseif (
is_object($data)) {
    if (!isset(
$data->RECURSIVE_REPLACE_MARKER)) {
     
$data->RECURSIVE_REPLACE_MARKER = TRUE;
      foreach(
$data as $key=>$val) {
        if(
is_array($data->$key) || is_object($data->$key)){
         
recursive_replace($data->$key, $find, $replace);
        }else{
          if(
$data->$key === $find) {
           
$data->$key = $replace;
          }
        }
      }
      unset(
$data->RECURSIVE_REPLACE_MARKER);
    }
  }
}
?>
To Top