Balaji Kandasamy
Balaji Kandasamy

Reputation: 4506

Merging two array elements into one array element in PHP

I want to merge two arrays into one array as follows,

Array1:

Array
(
    [0] => Array
        (
            [id] => 3
            [sku] => KOG456
            [cart_id] => 2
            [name] => Young Money
            [slug] => young-money
            [route_id] => 47
            [description] => 
This is test song


            [excerpt] => 
            [saleprice] => 90.00
            [related_products] => 
            [images] => {"1c6b0883fc94c5f644497ec488cdf8cb":{"filename":"1c6b0883fc94c5f644497ec488cdf8cb.jpg","alt":"Test","caption":"","primary":true}}
            [seo_title] => 
            [meta] => 
            [enabled] => 1
        )

)

Array2:

Array
(
    [0] => Array
        (
            [filename] => Beethovens_Symphony_No._9_(Scherzo).wma
            [title] => Young Money
            [size] => 599.26
        )

)

Expected array result is:

Array
(
    [0] => Array
        (
            [id] => 3
            [sku] => KOG456
            [cart_id] => 2
            [name] => Young Money
            [slug] => young-money
            [route_id] => 47
            [description] => 
This is test song


            [excerpt] => 
            [saleprice] => 90.00
            [related_products] => 
            [images] => {"1c6b0883fc94c5f644497ec488cdf8cb":{"filename":"1c6b0883fc94c5f644497ec488cdf8cb.jpg","alt":"Test","caption":"","primary":true}}
            [seo_title] => 
            [meta] => 
            [enabled] => 1
            [filename] => Beethovens_Symphony_No._9_(Scherzo).wma
            [title] => Young Money
            [size] => 599.26
        )

)

How to merge these array elements into one array element ?

Upvotes: 2

Views: 12856

Answers (6)

bwoebi
bwoebi

Reputation: 23777

foreach ($origArray as $key => &$subArray)
    $subArray += $arrayToBeAdded[$key];

Where $origArray is your array which is to be merged into and $arrayToBeAdded the array you merge into.

Upvotes: 9

brbcoding
brbcoding

Reputation: 13596

Since you have unique keys, you could use something as simple as the + operator (union)...

For example:

$arr1 = [1=>'testing',2=>'stack',3=>'overflow'];
$arr2 = [4=>'something',5=>'else',6=>'here'];
$arr3 = $arr1 + $arr2;
print_r($arr3);

Results:
Array ( [1] => testing [2] => stack [3] => overflow [4] => something [5] => else [6] => here )

Upvotes: 0

Ganesh Kanawade
Ganesh Kanawade

Reputation: 381

Use function array_merge($array1[0], $array2[0]) . Following is the example for the same

$array1 = array(0=>array('1'=>1,'2'=>2,'3'=>3));

$array2 = array(0=>array('4'=>4,'5'=>5,'6'=>6));

$result[0] = array_merge($array1[0],$array2[0]);

echo '<pre>';

print_r($result);

Upvotes: 0

Bart Roelofs
Bart Roelofs

Reputation: 441

For this php has multiple functions. You can use $arrays = array_combine($array1, $array2);.

PHP.net - array_combine

Hope it helped!

Upvotes: -1

linepogl
linepogl

Reputation: 9335

Try this little known overload of the + operator for arrays:

$result = $array1[0] + $array2[0]

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

User array_merge_recursive():

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

Upvotes: 1

Related Questions