3

$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
        )
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
jidhin dr
  • 33
  • 1
  • 4
  • possible duplicate of [Combine two arrays](http://stackoverflow.com/questions/6535444/combine-two-arrays) – viral Jul 06 '15 at 07:01

2 Answers2

4

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

Community
  • 1
  • 1
DavidDomain
  • 14,976
  • 4
  • 42
  • 50
2
  $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 merge :

$result = array_merge($array1, $array2);

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

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • 1
    This will not work in this case because the same string keys. The later value for that key will overwrite the previous one. – DavidDomain Jul 06 '15 at 07:17
  • @DavidDomain you are right, for this reason you need array_merge_recursive function. I edited my answer. And also need to chage his array structure properly. – Desert_king Jul 06 '15 at 08:12
  • That would be an option. I deleted the down vote, but i would still prefer `array_merge_recursive`. We have to consider that the OP does not want to change the array structure. – DavidDomain Jul 06 '15 at 08:25