Reputation: 563
below is my array , i want to sort in by price in asc order.
i wrote below code
Array
(
[105493] => Array
(
[info] => Array
(
[price] => $50.00
[hazmat] => Not Required
)
)
[105494] => Array
(
[info] => Array
(
[price] => $93.60
[hazmat] => Not Required
)
)
[105495] => Array
(
[info] => Array
(
[price] => $198.00
[hazmat] => Not Required
)
)
[105496] => Array
(
[info] => Array
(
[price] => $662.00
[hazmat] => Not Required
)
)
)
i worte below code to sort it
function customer_sort ($a, $b) {
if ($a['info']['price'] == $b['info']['price']) {
return 0;
}
return $a['info']['price'] > $b['info']['price'] ? 1 : -1;
}
uasort($assc_product_data, 'customer_sort');
but my code is not woking fine how can i solve issue
Upvotes: 0
Views: 43
Reputation: 780899
You need to remove the $
before comparing the prices:
function customer_sort($a, $b)
if ($a['info']['price'] == $b['info']['price']) {
return 0;
}
return substr($a['info']['price'], 1) > substr($b['info']['price'], 1) ? 1 : -1;
}
You could also use the built-in function strnatcmp
:
function customer_sort($a, $b) {
return strnatcmp($a['info']['price'], $b['info']['price']);
}
Upvotes: 2