Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge multiple arrays with sorting order PHP

Tags:

php

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.

like image 774
piyush Avatar asked Jan 17 '26 22:01

piyush


1 Answers

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));
like image 89
Oliver Maksimovic Avatar answered Jan 20 '26 12:01

Oliver Maksimovic