简体   繁体   中英

PHP merge two associative arrays

$array 1:-

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
        )
)

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);

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() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. 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 . As the PHP docs state

...the values for these keys are merged together into an array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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