Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to merge these arrays in PHP? [duplicate]

Tags:

arrays

php

Possible Duplicate:
PHP: Merge 2 Multidimensional Arrays

I have these arrays and I want to merge them into one array.

$arrayAAA[0]['name'] = "stackoverflow";
$arrayBBB[0]['color'] = "white";
$arrayCCC[0]['media'] = "web";

I want to merge these like this.

$newArray[0]['name'] //"stackoverflow"
$newArray[0]['color'] //"white"
$newArray[0]['media'] //"web"

If anyone knows how to do this, please give me a help. I thought I could merge them by using array_merge(), but this function doesn't work in my case.

Thanks so much in advance!

like image 827
crzyonez777 Avatar asked Dec 19 '12 00:12

crzyonez777


3 Answers

I dont know how much time you have wasted finding the solution while you could have written a manual one.

foreach(array($arrayAAA, $arrayBBB, $arrayCCC) as $v){
    foreach($v as $iv){
        $result[key($iv)] = $iv[key($iv)];
    }
}

CodePad

like image 170
Shiplu Mokaddim Avatar answered Nov 02 '22 23:11

Shiplu Mokaddim


I think you want to use array_merge(), not merge_array()

like image 32
Sterling Archer Avatar answered Nov 02 '22 23:11

Sterling Archer


So, this does not work either?

$x = array();
$x[0] = array_merge($arrayA[0], $arrayB[0], ...);

There is also array_merge_recursive function. But I am pretty sure it would only append each sub-array.

like image 34
dualed Avatar answered Nov 02 '22 23:11

dualed