I want to club multiple arrays into a single associate array
like
$big=array(
1=>$simpleArray1,
2=>$simpleArray2,
3=>$singleArray3
);
I know that the above way of assignment is wrong, it does not actually club the arrays. Please recommend me the best way. Thanks :)
EDIT
All those arrays have same keys, so how I should proceed ahead in this case. I understand that keys are meant to be identical, but what do I do now! I'm confused because I need to club n
number of arrays with different volumes of data in each
Do you want to merge arrays?
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
Result:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
http://php.net/manual/en/function.array-merge.php
EDIT
All those arrays have same keys, so how I should proceed ahead in this case. I understand that keys are meant to be identical, but what do I do now! I'm confused because I need to club n number of arrays with different volumes of data in each
So how do you want it to look?
you can do also
$all = array();
$all[] = $firstarray;
$all[] = $secondarray;
$all[] = $thirdarray;
but than you must do twice foreach, and you cannot search by id
foreach($all as $arr){
foreach($arr as $item){
echo $item["name"];
}
}
Main question is, what do you want to do with the aggregate array. if you do not care about the keys the worst case scenario is
$arr = array();
foreach($arr1 as $v){$arr[] = $v;}
foreach($arr2 as $v){$arr[] = $v;}
foreach($arr3 as $v){$arr[] = $v;}
You can use array_merge($arr1, $arr2, ...)
. See here and notice that identical keys will be overwritten by the latest ones.