arrays - php - sorting function does not sort indicies -
i using following function sort array
function array_sort($array, $on, $order=sort_asc){ $new_array = array(); $sortable_array = array(); if (count($array) > 0) { foreach ($array $k => $v) { if (is_array($v)) { foreach ($v $k2 => $v2) { if ($k2 == $on) { $sortable_array[$k] = $v2; } } } else { $sortable_array[$k] = $v; } } switch ($order) { case sort_asc: asort($sortable_array); break; case sort_desc: arsort($sortable_array); break; } foreach ($sortable_array $k => $v) { $new_array[$k] = $array[$k]; } } return $new_array; } $prevnext = array_sort($prevnext, 'distance-between', sort_asc);
when var_dump();
sorted array ($prevnext), expected result, when var_dump($prevnext[0]);
, not give me want, rather, data before sorted.
have misunderstood sorting? there way re order array based on specific value of key. when use $prevnext[0]
, re-ordered data. appreciated.
var_dump($prevnext);
gives array below, sorted using distance-between key:
array(11) { [10]=> array(7) { [0]=> string(3) "284" ["c_service_id"]=> string(3) "284" [1]=> string(14) "mcgill buckley" ["service_name"]=> string(14) "mcgill buckley" [2]=> string(7) "k2b 6v2" ["service_zip"]=> string(7) "k2b 6v2" ["distance-between"]=> float(9.24) } [0]=> array(7) { [0]=> string(4) "1319" ["c_service_id"]=> string(4) "1319" [1]=> string(38) "mds macfarlane data & mailing services" ["service_name"]=> string(38) "mds macfarlane data & mailing services" [2]=> string(7) "k1b 3v9" ["service_zip"]=> string(7) "k1b 3v9" ["distance-between"]=> float(11.66) } [6]=> array(7) { [0]=> string(4) "1270" ["c_service_id"]=> string(4) "1270" [1]=> string(38) "mds macfarlane data & mailing services" ["service_name"]=> string(38) "mds macfarlane data & mailing services" [2]=> string(7) "k1b 3v9" ["service_zip"]=> string(7) "k1b 3v9" ["distance-between"]=> float(11.66) } [7]=> array(7) { [0]=> string(4) "1268" ["c_service_id"]=> string(4) "1268" [1]=> string(31) "evoke direct response solutions" ["service_name"]=> string(31) "evoke direct response solutions" [2]=> string(7) "k2m 1x5" ["service_zip"]=> string(7) "k2m 1x5" ["distance-between"]=> float(13.48) } [3]=> array(7) { [0]=> string(4) "1293" ["c_service_id"]=> string(4) "1293" [1]=> string(24) "creative signs & designs" ["service_name"]=> string(24) "creative signs & designs" [2]=> string(7) "k1z 7s8" ["service_zip"]=> string(7) "k1z 7s8" ["distance-between"]=> float(27.43) } [9]=> array(7) { [0]=> string(3) "289" ["c_service_id"]=> string(3) "289" [1]=> string(8) "seoplus+" ["service_name"]=> string(8) "seoplus+" [2]=> string(7) "k2b 1a5" ["service_zip"]=> string(7) "k2b 1a5" ["distance-between"]=> float(32.32) } [5]=> array(7) { [0]=> string(4) "1275" ["c_service_id"]=> string(4) "1275" [1]=> string(35) "innovacom marketing & communication" ["service_name"]=> string(35) "innovacom marketing & communication" [2]=> string(7) "k1m 1l5" ["service_zip"]=> string(7) "k1m 1l5" ["distance-between"]=> float(43.62) } [8]=> array(7) { [0]=> string(4) "1267" ["c_service_id"]=> string(4) "1267" [1]=> string(27) "rideau mailing services inc" ["service_name"]=> string(27) "rideau mailing services inc" [2]=> string(7) "k2e 7k1" ["service_zip"]=> string(7) "k2e 7k1" ["distance-between"]=> float(45.74) } [2]=> array(7) { [0]=> string(4) "1294" ["c_service_id"]=> string(4) "1294" [1]=> string(21) "agent signs & designs" ["service_name"]=> string(21) "agent signs & designs" [2]=> string(7) "k1y 0t8" ["service_zip"]=> string(7) "k1y 0t8" ["distance-between"]=> float(50.11) } [1]=> array(7) { [0]=> string(4) "1297" ["c_service_id"]=> string(4) "1297" [1]=> string(21) "golden triangle signs" ["service_name"]=> string(21) "golden triangle signs" [2]=> string(7) "k0a 1b0" ["service_zip"]=> string(7) "k0a 1b0" ["distance-between"]=> float(57.39) } [4]=> array(7) { [0]=> string(4) "1280" ["c_service_id"]=> string(4) "1280" [1]=> string(32) "aditek design printing , signs" ["service_name"]=> string(32) "aditek design printing , signs" [2]=> string(7) "k1g 3w3" ["service_zip"]=> string(7) "k1g 3w3" ["distance-between"]=> float(64.13) } }
but when use $prevnext[0]
, not give me same result above. get:
array(7) { [0]=> string(4) "1319" ["c_service_id"]=> string(4) "1319" [1]=> string(38) "mds macfarlane data & mailing services" ["service_name"]=> string(38) "mds macfarlane data & mailing services" [2]=> string(7) "k1b 3v9" ["service_zip"]=> string(7) "k1b 3v9" ["distance-between"]=> float(11.66) }
where should getting:
array(7) { [0]=> string(3) "284" ["c_service_id"]=> string(3) "284" [1]=> string(14) "mcgill buckley" ["service_name"]=> string(14) "mcgill buckley" [2]=> string(7) "k2b 6v2" ["service_zip"]=> string(7) "k2b 6v2" ["distance-between"]=> float(9.24) }
the array above should shown when var_dump($prevnext[0]);
first has lowest distance-between.
used following sort array indices
function sort_by_order ($a, $b) { return $a['order'] - $b['order']; } usort($array, 'sort_by_order ');
Comments
Post a Comment