php - sort associative multi array by key value -


below array , want sort in 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 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 code not woking fine how can solve issue

you need remove $ before comparing 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 use built-in function strnatcmp:

function customer_sort($a, $b) {     return strnatcmp($a['info']['price'], $b['info']['price']); } 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -