Reputation: 554
I have multiple arrays like.
$arr1 = array('11','25','363','434','333');
$arr2 = array('11','265','343','424','333');
$arr3 = array('18','235','33','4454','3354');
$arr4 = array('171','245','33','424','353');
How can i merge multiple arrays dynamically?
After mergeing all arrays I want this output to be sorted.
Upvotes: 2
Views: 156
Reputation: 994
Use this code -
<?php
$arr1 = array('11', '25', '363', '434', '333');
$arr2 = array('11', '265', '343', '424', '333');
$arr3 = array('18', '235', '33', '4454', '3354');
$arr4 = array('171', '245', '33', '424', '353');
$am = array_unique(array_merge($arr1, $arr2, $arr3, $arr4));
sort($am);
$arrlength = count($am);
for ($x=0; $x < $arrlength; $x++) {
echo $am[$x];
echo "<br>";
}
?>
Upvotes: 2
Reputation: 3262
It's as simple as it sounds:
$result = array_merge($arr1, $arr2, $arr3, $arr4);
sort($result);
var_dump($result);
Optionally, if you need to get rid of the duplicate values in the resulting array, use:
$result = array_unique(array_merge($arr1, $arr2, $arr3, $arr4));
Upvotes: 7
Reputation: 383
I have tested the code here. Hope this helps.
<?php
$arr1 = array('11','25','363','434','333');
$arr2 = array('11','265','343','424','333');
$arr3 = array('18','235','33','4454','3354');
$arr4 = array('171','245','33','424','353');
$merged = array_merge($arr1,$arr2,$arr3,$arr4);
sort($merged, SORT_NUMERIC);
foreach ($merged as $key => $val) {
echo "array[" . $key . "] = " . $val . "\n";
}
Upvotes: 4