arunavak111

Untitled

Feb 21st, 2014
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.19 KB | None | 0 0
  1. $inventory = array(
  2.  
  3.    array("type"=>"fruit", "price"=>3.50),
  4.    array("type"=>"milk", "price"=>2.90),
  5.    array("type"=>"pork", "price"=>5.43),
  6.  
  7. );
  8. $price = array();
  9. foreach ($inventory as $key => $row)
  10. {
  11.     $price[$key] = $row['price'];
  12. }
  13. echo 'Before Sort';
  14. echo '<pre>';print_r($inventory);echo '</pre>';
  15. echo 'Price array()';
  16. echo '<pre>';print_r($price);echo '</pre>';
  17. array_multisort($price, SORT_DESC, $inventory);
  18. echo '<pre>';print_r($inventory);echo '</pre>';
  19.  
  20.  
  21. // result
  22.  
  23. Before Sort
  24.  
  25. Array
  26. (
  27.     [0] => Array
  28.         (
  29.             [type] => fruit
  30.             [price] => 3.5
  31.         )
  32.  
  33.     [1] => Array
  34.         (
  35.             [type] => milk
  36.             [price] => 2.9
  37.         )
  38.  
  39.     [2] => Array
  40.         (
  41.             [type] => pork
  42.             [price] => 5.43
  43.         )
  44.  
  45. )
  46.  
  47. Price array()
  48.  
  49. Array
  50. (
  51.     [0] => 3.5
  52.     [1] => 2.9
  53.     [2] => 5.43
  54. )
  55.  
  56. Array
  57. (
  58.     [0] => Array
  59.         (
  60.             [type] => pork
  61.             [price] => 5.43
  62.         )
  63.  
  64.     [1] => Array
  65.         (
  66.             [type] => fruit
  67.             [price] => 3.5
  68.         )
  69.  
  70.     [2] => Array
  71.         (
  72.             [type] => milk
  73.             [price] => 2.9
  74.         )
  75.  
  76. )
Advertisement
Add Comment
Please, Sign In to add comment