Merge two object arrays and remove duplicated objects then sort by column value
arrays, duplicates, object, php, sorting
Solution
These 3 simple steps did the work:
//both arrays will be merged including duplicates
$result = array_merge( $array1, $array2 );
//duplicate objects will be removed
$result = array_map("unserialize", array_unique(array_map("serialize", $result)));
//array is sorted on the bases of id
sort( $result );
Note: Answer by @Kamran helped me come to this simple solution
Problem
I have the following two arrays of objects: First Array: `$array1` ``` Array ( [0] => stdClass Object ( [id] => 100 [name] => Muhammad ) [1] => stdClass Object ( [id] => 102 [name] => Ibrahim ) [2] => stdClass Object ( [id] => 101 [name] => Sumayyah ) ) ``` Second Array: `$array2` ``` Array ( [0] => stdClass Object ( [id] => 100 [name] => Muhammad ) [1] => stdClass Object ( [id] => 103 [name] => Yusuf ) ) ``` I want to merge these two object arrays (removing all duplicates) and sorted according to `id`. Desired output: ``` Array ( [0] => stdClass Object ( [id] => 100 [name] => Muhammad ) [1] => stdClass Object ( [id] => 101 [name] => Sumayyah ) [2] => stdClass Object ( [id] => 102 [name] => Ibrahim ) [3] => stdClass Object ( [id] => 103 [name] => Yusuf ) ) ```