简体   繁体   English

我想在保留数组键的同时合并两个 arrays

[英]I want to merge two arrays while keeping the array keys

I want to merge two arrays while keeping the array keys我想在保留数组键的同时合并两个 arrays

I want to create $array_true merging $array_1 and $array_2 as follows.我想创建 $array_true 合并 $array_1 和 $array_2 如下。

$array_1 = array(
    'some_one' => 'some_text',
    'array_1'  => array(
        'value_1_1',
        'value_1_2'
    ),

);

$array_2 = array(
    'some_one' => 'some_text_2',
    'array_0'  => array(
        'value_0_1',
        'value_0_2'
    ),
    'array_1'  => array(
        'value_defaults_1_1',
        'value_defaults_1_2'
    ),
    'array_2'  => array(
        'value_defaults_2_1',
        'value_defaults_2_2'
    ),
);

// Ultimately, I would like to create such a function.
$array_true = array(
    'some_one' => 'some_text',
    'array_0'  => array(
        'value_0_1',
        'value_0_2'
    ),
    'array_1'  => array(
        'value_1_1',
        'value_1_2'
    ),
    'array_2'  => array(
        'value_defaults_2_1',
        'value_defaults_2_2'
    ),
);

function my_merge_func($array_1, $array_2) {
    $merged = $array_1;
    foreach ($array_2 as $key => $value) {
        if (
            empty($array_1[ $key ])
        ) {
            $merged[ $key ] = $value;
        } else if(is_array($value) && is_array($array_1[ $key ])) {
            $merged[ $key ] = my_merge_func($array_1[ $key ], $value);
        }
    }
    return $merged;
}
var_dump(my_merge_func($array_1,$array_2));

var_dump($array_true);

var_dump(my_merge_func($array_1,$array_2) ===  $array_true);

my_merge_func will be the one I created in the previous question my_merge_func 将是我在上一个问题中创建的那个

Functions like array_merge with recursive processing 具有递归处理的 array_merge 等函数

As it is now, the array looks like this.就像现在一样,数组看起来像这样。

array(4) {
  ["some_one"]=>
  string(9) "some_text"
  ["array_1"]=>
  array(2) {
    [0]=>
    string(9) "value_1_1"
    [1]=>
    string(9) "value_1_2"
  }
  ["array_0"]=>
  array(2) {
    [0]=>
    string(9) "value_0_1"
    [1]=>
    string(9) "value_0_2"
  }
  ["array_2"]=>
  array(2) {
    [0]=>
    string(18) "value_defaults_2_1"
    [1]=>
    string(18) "value_defaults_2_2"
  }
}

I would appreciate your advice.我会很感激你的建议。

The issue isn't with the function you've created;问题不在于您创建的 function ; that works exactly like it should.这完全像它应该的那样工作。 The problem is your very last line of code:问题是您的最后一行代码:

var_dump(my_merge_func($array_1,$array_2) ===  $array_true);

Of course, you'd just like to check whether the resulting array matches your desired result.当然,您只想检查结果数组是否与您想要的结果匹配。 If you have a look at the PHP manual for the array comparison operators, you'll read the following for the triple equals ( === ) operator:如果您查看有关数组比较运算符的PHP 手册,您将阅读以下有关三等号 ( === ) 运算符的内容:

true if $a and $b have the same key/value pairs in the same order and of the same types.如果$a$b具有相同顺序和相同类型的相同键/值对,则为true

While all of your value pairs have the exact same key/value-pairs and types, the order is not the same, which is why the === returns false.虽然您的所有值对都具有完全相同的键/值对和类型,但顺序并不相同,这就是===返回 false 的原因。 Functionally, the arrays are exaclty the same and the order does not matter at all.功能上,arrays 完全一样,顺序根本不重要。 The quick fix is to change to a double equals:快速解决方法是更改为双等于:

var_dump(my_merge_func($array_1,$array_2) == $array_true);

It doesn't account for type safety, but it does the trick for this application.它不考虑类型安全,但它为这个应用程序解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM