I have array that I need to sort by another array form 2 fields zip code and approve
I am able to sort it by zip code but unable to do it with approved field so for eg
I need to sort by 60007,60001,60003,60002 (as per sortlike order) all zip code from 60007 and approved should come first so
$sortLike=array(60007,60001,60003,60002);
$array1= array(
array ('ID' => 138,'zip_code' => 60007,'approved' => 1),
array('ID' => 103,'zip_code' => 60007,'approved' => 0),
array('ID' => 114,'zip_code' => 60007,'approved' => 1),
array('ID' => 105,'zip_code' => 60003,'approved' => 0),
array('ID' => 124,'zip_code' => 60002,'approved' => 0)
)
so 60007 and aproved should come first than 60007 with 0(unapproved) and than all 60001 approved than all 60001 unapproved(and so on as per $sortlike
) here is complete php code
<?php
$array1= array(
array ('ID' => 138,'zip_code' => 60007,'approved' => 1),
array('ID' => 103,'zip_code' => 60007,'approved' => 0),
array('ID' => 114,'zip_code' => 60007,'approved' => 1),
array('ID' => 105,'zip_code' => 60003,'approved' => 0),
array('ID' => 124,'zip_code' => 60002,'approved' => 0),
array('ID' => 104,'zip_code' => 60002,'approved' => 1),
array('ID' => 106,'zip_code' => 60001,'approved' => 0),
array('ID' => 188,'zip_code' => 60022,'approved' => 0),
array('ID' => 184,'zip_code' => 60022,'approved' => 1),
);
function sort_results ($a, $b) {
$sortLike=array(60007,60001,60003,60002);
if (!in_array ($a['zip_code'], $sortLike)) { return 1; } // Push unknown values at the end of the array
return array_search($a['zip_code'], $sortLike) > array_search($b['zip_code'], $sortLike);
}
usort ($array1, 'sort_results');
echo "<pre>";
print_r($array1);
echo "</pre>";