Php 正在丢失与数组合并的值

Php 正在丢失与数组合并的值,php,arrays,array-merge,Php,Arrays,Array Merge,我有一个数组: { "zone1":{ "foo1":"bar1", "foo2":"bar2", "foo3":"bar3", "foo4":"bar4" }, "zone2":{ "newfoo1":"newbar1", "newfoo2":"newbar2", "newfoo3":"newbar3", "newfoo4":"newbar4" }, "zone3":{ "morefoo1":"more

我有一个数组:

{
  "zone1":{
    "foo1":"bar1",
    "foo2":"bar2",
    "foo3":"bar3",
    "foo4":"bar4"
  },
  "zone2":{
    "newfoo1":"newbar1",
    "newfoo2":"newbar2",
    "newfoo3":"newbar3",
    "newfoo4":"newbar4"
  },
  "zone3":{
    "morefoo1":"morebar1",
    "morefoo2":"morebar2",
    "morefoo3":"morebar3",
    "morefoo4":"morebar4"
  }
}
我想用更新的值合并第二个数组:

{
  "zone1":{
    "foo1":"updatedbar1"
  },
  "zone3":{
    "morefoo2":"updatedbar2",
    "morefoo4":"updatedbar4"
  }
}
我已经尝试了很多东西,我现在使用的是以下php代码:

$array3 = array_merge($array1, $array2);
但这段代码告诉我:

{
  "zone1":{
    "foo1":"updatedbar1"
  },
  "zone2":{
    "newfoo1":"newbar1",
    "newfoo2":"newbar2",
    "newfoo3":"newbar3",
    "newfoo4":"newbar4"
  },
  "zone3":{
    "morefoo2":"updatedbar2",
    "morefoo4":"updatedbar4"
  }
}
我想要的只是用第二个数组上的值更新第一个数组,而不丢失任何数据。 数组是json,它们来自json文件,但语言是PHP。

array\u merge()只合并顶级数组,不考虑数组中的数组。因此,后一个数组将覆盖重复的值

您可以使用
array\u merge\u recursive()解决此问题。

有关更多信息,请访问

-- 编辑

上面提到的页面上的第一条注释解释了如何合并两个数组,同时使用第二个数组更新第一个数组:

<?php
/**
* array_merge_recursive does indeed merge arrays, but it converts values with duplicate
* keys to arrays rather than overwriting the value in the first array with the duplicate
* value in the second array, as array_merge does. I.e., with array_merge_recursive,
* this happens (documented behavior):
*
* array_merge_recursive(array('key' => 'org value'), array('key' => 'new value'));
*     => array('key' => array('org value', 'new value'));
*
* array_merge_recursive_distinct does not change the datatypes of the values in the     arrays.
* Matching keys' values in the second array overwrite those in the first array, as is     the
* case with array_merge, i.e.:
*
* array_merge_recursive_distinct(array('key' => 'org value'), array('key' => 'new value'));
*     => array('key' => array('new value'));
*
* Parameters are passed by reference, though only for performance reasons. They're     not
* altered by this function.
*
* @param array $array1
* @param array $array2
* @return array
* @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk>
* @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com>
*/
function array_merge_recursive_distinct ( array &$array1, array &$array2 )
{
  $merged = $array1;

  foreach ( $array2 as $key => &$value )
  {
    if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) )
    {
      $merged [$key] = array_merge_recursive_distinct ( $merged [$key], $value );
    }
    else
    {
  $merged [$key] = $value;
    }
  }

  return $merged;
}
?>

array\u merge()只合并顶级数组,不考虑数组中的数组。因此,后一个数组将覆盖重复的值

您可以使用
array\u merge\u recursive()解决此问题。

有关更多信息,请访问

-- 编辑

上面提到的页面上的第一条注释解释了如何合并两个数组,同时使用第二个数组更新第一个数组:

<?php
/**
* array_merge_recursive does indeed merge arrays, but it converts values with duplicate
* keys to arrays rather than overwriting the value in the first array with the duplicate
* value in the second array, as array_merge does. I.e., with array_merge_recursive,
* this happens (documented behavior):
*
* array_merge_recursive(array('key' => 'org value'), array('key' => 'new value'));
*     => array('key' => array('org value', 'new value'));
*
* array_merge_recursive_distinct does not change the datatypes of the values in the     arrays.
* Matching keys' values in the second array overwrite those in the first array, as is     the
* case with array_merge, i.e.:
*
* array_merge_recursive_distinct(array('key' => 'org value'), array('key' => 'new value'));
*     => array('key' => array('new value'));
*
* Parameters are passed by reference, though only for performance reasons. They're     not
* altered by this function.
*
* @param array $array1
* @param array $array2
* @return array
* @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk>
* @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com>
*/
function array_merge_recursive_distinct ( array &$array1, array &$array2 )
{
  $merged = $array1;

  foreach ( $array2 as $key => &$value )
  {
    if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) )
    {
      $merged [$key] = array_merge_recursive_distinct ( $merged [$key], $value );
    }
    else
    {
  $merged [$key] = $value;
    }
  }

  return $merged;
}
?>

您可以使用:

它使用您的数据创建以下数组:

Array
(
    [zone1] => Array
        (
            [foo1] => updatedbar1
            [foo2] => bar2
            [foo3] => bar3
            [foo4] => bar4
        )

    [zone2] => Array
        (
            [newfoo1] => newbar1
            [newfoo2] => newbar2
            [newfoo3] => newbar3
            [newfoo4] => newbar4
        )

    [zone3] => Array
        (
            [morefoo1] => morebar1
            [morefoo2] => updatedbar2
            [morefoo3] => morebar3
            [morefoo4] => updatedbar4
        )

)
请在上查看。

您可以使用:

它使用您的数据创建以下数组:

Array
(
    [zone1] => Array
        (
            [foo1] => updatedbar1
            [foo2] => bar2
            [foo3] => bar3
            [foo4] => bar4
        )

    [zone2] => Array
        (
            [newfoo1] => newbar1
            [newfoo2] => newbar2
            [newfoo3] => newbar3
            [newfoo4] => newbar4
        )

    [zone3] => Array
        (
            [morefoo1] => morebar1
            [morefoo2] => updatedbar2
            [morefoo3] => morebar3
            [morefoo4] => updatedbar4
        )

)

查看它。

什么编程语言?对
array\u merge
的调用显然是PHP,但是您的数组在这里显示为JSON。请澄清。是的,我的数组来自使用json_解码和json_编码的json文件。我的语言是PHP。如果您显示PHP数组,它将比json数组更有用,或者只在每个数组变量之前添加
(数组)
。什么编程语言?对
array\u merge
的调用显然是PHP,但您的数组在这里显示为json。请澄清。是的,我的数组来自使用json_解码和json_编码的json文件。我的语言是PHP。如果你展示了PHP数组,它会比json数组更有用,或者只是在每个数组变量之前添加
(数组)
。它是递归的,我尝试过它,两个数组中的值不会被覆盖,而是一个一个,如下所示:“newfoo1”:[“newbar1”,“updatedbar1”]似乎正在使用“array\u replace\u recursive”,明天我将进行更多测试,如果失败,我将尝试您的编辑,谢谢。它是递归的,我尝试过它,两个数组中的值将不会被覆盖,将是一个相互的,如:“newfoo1”:[“newbar1”,“updatedbar1”]似乎正在使用“array\u replace\u recursive”,明天我会做更多的测试,如果失败,我会尝试你的编辑,谢谢。