PHP 中关联数组的数组合并

发布于 2024-11-26 13:20:29 字数 378 浏览 0 评论 0原文

我如何在关联数组上进行 array_merge,如下所示:

Array 1:

$options = array (
"1567" => "test",
"1853" => "test1",
);

Array 2:

$option = array (
"none" => "N/A"
);

所以我需要 array_merge 这两个,但是当我这样做时,我得到了这个(在调试中):

Array
(
    [none] => N/A
    [0] => test
    [1] => test1
)

How can i do an array_merge on an associative array, like so:

Array 1:

$options = array (
"1567" => "test",
"1853" => "test1",
);

Array 2:

$option = array (
"none" => "N/A"
);

So i need to array_merge these two but when i do i get this (in debug):

Array
(
    [none] => N/A
    [0] => test
    [1] => test1
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

撩人痒 2024-12-03 13:20:29

尝试使用:

$finalArray = $options + $option。参见http://codepad.org/BJ0HVtac
只需检查重复键的行为,我没有对此进行测试。对于唯一的键,它效果很好。

<?php

$options = array (
                  "1567" => "test",
                  "1853" => "test1",
                  );


$option = array (
"none" => "N/A"
);


$final = array_merge($option,$options);

var_dump($final);


$finalNew = $option + $options ;

var_dump($finalNew);

?>

try using :

$finalArray = $options + $option .see http://codepad.org/BJ0HVtac
Just check the behaviour for duplicate keys, I did not test this. For unique keys, it works great.

<?php

$options = array (
                  "1567" => "test",
                  "1853" => "test1",
                  );


$option = array (
"none" => "N/A"
);


$final = array_merge($option,$options);

var_dump($final);


$finalNew = $option + $options ;

var_dump($finalNew);

?>
说谎友 2024-12-03 13:20:29

只需使用$options + $option

var_dump($options + $option);

输出:

array(3) {
  [1567]=>
  string(4) "test"
  [1853]=>
  string(5) "test1"
  ["none"]=>
  string(3) "N/A"
}

但是当发生按键碰撞时要小心。这是PHP手册所说的:

第一个数组中的键将被保留。 如果两个数组中都存在数组键,则将使用第一个数组中的元素,并忽略第二个数组中匹配键的元素。

Just use $options + $option!

var_dump($options + $option);

outputs:

array(3) {
  [1567]=>
  string(4) "test"
  [1853]=>
  string(5) "test1"
  ["none"]=>
  string(3) "N/A"
}

But be careful when there is a key collision. Here is what the PHP manual says:

The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored.

爱已欠费 2024-12-03 13:20:29
$final_option = $option + $options;
$final_option = $option + $options;
橘香 2024-12-03 13:20:29

我希望将两个关联数组合并在一起,如果键相同,则将值添加在一起。如果任一数组都有唯一的键,这些键将与其现有值一起添加到合并数组中。

我找不到执行此操作的函数,所以做了这个:

function array_merge_assoc($array1, $array2)
{
     
    if(sizeof($array1)>sizeof($array2))
    {
        echo $size = sizeof($array1);
    }
    else
    {
        $a = $array1;
        $array1 = $array2;
        $array2 = $a;
         
        echo $size = sizeof($array1);
    }
     
    $keys2 = array_keys($array2);
    
    for($i = 0;$i<$size;$i++)
    {
        $array1[$keys2[$i]] = $array1[$keys2[$i]] + $array2[$keys2[$i]];
    }
     
    $array1 = array_filter($array1);
    return $array1;
 }

参考:http://www.php.net/manual/en/function.array-merge.php#90136

I was looking to merge two associative arrays together, adding the values together if the keys were the same. If there were keys unique to either array, these would be added into the merged array with their existing values.

I couldnt find a function to do this, so made this:

function array_merge_assoc($array1, $array2)
{
     
    if(sizeof($array1)>sizeof($array2))
    {
        echo $size = sizeof($array1);
    }
    else
    {
        $a = $array1;
        $array1 = $array2;
        $array2 = $a;
         
        echo $size = sizeof($array1);
    }
     
    $keys2 = array_keys($array2);
    
    for($i = 0;$i<$size;$i++)
    {
        $array1[$keys2[$i]] = $array1[$keys2[$i]] + $array2[$keys2[$i]];
    }
     
    $array1 = array_filter($array1);
    return $array1;
 }

Reference: http://www.php.net/manual/en/function.array-merge.php#90136

得不到的就毁灭 2024-12-03 13:20:29

当 array_merge 不起作用时,只需

<?php
$new = array();
foreach ($options as $key=>$value) $new[$key] = $value;
foreach ($option as $key=>$value) $new[$key] = $value;
?>

根据哪个数组具有更高优先级执行或切换两个 foreach 循环

when array_merge doesn't work, then simply do

<?php
$new = array();
foreach ($options as $key=>$value) $new[$key] = $value;
foreach ($option as $key=>$value) $new[$key] = $value;
?>

or switch the two foreach loops depending on which array has higher priority

日暮斜阳 2024-12-03 13:20:29

此代码可用于递归合并:

function merge($arr1, $arr2){
        $out = array();
        foreach($arr1 as $key => $val1){
            if(isset($arr2[$key])){
                if(is_array($arr1[$key]) && is_array($arr2[$key])){
                    $out[$key]=  merge($arr1[$key], $arr2[$key]);
                }else{
                    $out[$key]= array($arr1[$key], $arr2[$key]);
                }
                unset($arr2[$key]);
            }else{
                $out[$key] = $arr1[$key];
            }
        }
        return $out + $arr2;
 }

This code could be used for recursive merge:

function merge($arr1, $arr2){
        $out = array();
        foreach($arr1 as $key => $val1){
            if(isset($arr2[$key])){
                if(is_array($arr1[$key]) && is_array($arr2[$key])){
                    $out[$key]=  merge($arr1[$key], $arr2[$key]);
                }else{
                    $out[$key]= array($arr1[$key], $arr2[$key]);
                }
                unset($arr2[$key]);
            }else{
                $out[$key] = $arr1[$key];
            }
        }
        return $out + $arr2;
 }
南城旧梦 2024-12-03 13:20:29

如果数组具有相同的键,则使用 array_merge_recursive() 。

$array1 = array( "a" => "1" , "b" => "45" );

$array2 = array( "a" => "23" , "b" => "33" );

$newarray = array_merge_recursive($array1,$array2);

array_merge_recursive() 不会覆盖,它只是将值作为数组。

If arrays having same keys then use array_merge_recursive()

$array1 = array( "a" => "1" , "b" => "45" );

$array2 = array( "a" => "23" , "b" => "33" );

$newarray = array_merge_recursive($array1,$array2);

The array_merge_recursive() wont overwrite, it just makes the value as an array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文