简体   繁体   English

PHP合并两个关联数组

[英]PHP merge two associative arrays

$array 1:- $ array 1: -

Array
(
    [Test Stock] => Array
        (
            [intStockCount] => 10
        )

    [CARTON 50 X 50 X 50] => Array
        (
            [intStockCount] => 10
        )
)

$array2:- $数组2: -

Array
(
    [Test Stock] => Array
        (
            [intInvoiceCount] => 20
        )

    [CARTON 50 X 50 X 50] => Array
        (
            [intInvoiceCount] => 30
        )
)

I need a new array combining all together without using loop 我需要一个新的数组组合在一起而不使用循环

Array
(
    [Test Stock] => Array
        (
            [intStockCount] => 10
            [intInvoiceCount] => 20
        )

    [CARTON 50 X 50 X 50] => Array
        (
            [intStockCount] => 10
            [intInvoiceCount] => 30
        )
)

  $array1 = array( 'Test Stock' => array( 'intStockCount' => 10, ), 'CARTON 50 X 50 X 50' => array( 'intStockCount' => 10 ) ); $array2 = array( 'Test Stock' => array( 'intStockCount' => 10, ), 'CARTON 50 X 50 X 50' => array( 'intStockCount' => 10 ) ); $result = array_merge_recursive($array1, $array1); var_dump($result); 

You have to change your array structure. 您必须更改阵列结构。

For normal array marge : $result = array_merge($array1, $array2); 对于普通数组marge:$ result = array_merge($ array1,$ array2);

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

You can use array_merge_recursive to do the job. 您可以使用array_merge_recursive来完成这项工作。

array_merge_recursive array_merge_recursive

array_merge_recursive() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. array_merge_recursive()将一个或多个数组的元素合并在一起,以便将一个值的值附加到前一个数组的末尾。 It returns the resulting array. 它返回结果数组。

If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. 如果输入数组具有相同的字符串键,则这些键的值将合并到一个数组中,这是递归完成的,因此如果其中一个值是数组本身,则该函数会将其与相应的条目合并在另一个数组中。 If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended. 但是,如果数组具有相同的数字键,则后面的值不会覆盖原始值,但会附加。

Just do: 做就是了:

<?php
$array1 = array(
    "Test Stock" => array(
        "intStockCount" => 10
    ),
    "CARTON 50 X 50 X 50" =>  array(
        "intStockCount" => 10
    )
);

$array2 = array(
    "Test Stock" => array(
        "intInvoiceCount" => 20
    ),
    "CARTON 50 X 50 X 50" =>  array(
        "intInvoiceCount" => 30
    )
);

$final = array_merge_recursive($array1,$array2);
echo '<pre>';
print_r($final);
echo '</pre>';

/* OUTPUT
Array
(
    [Test Stock] => Array
        (
            [intStockCount] => 10
            [intInvoiceCount] => 20
        )

    [CARTON 50 X 50 X 50] => Array
        (
            [intStockCount] => 10
            [intInvoiceCount] => 30
        )

)
*/

The important thing to notice here is that you are using the same string key in both of your arrays . 这里需要注意的重要一点是,您在两个arrays都使用相同的字符串key As the PHP docs state 正如PHP文档所述

...the values for these keys are merged together into an array ...这些键的值合并为一个数组

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

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