-1

I want to sort in ascending order field available_price in an array, how can I sort. Following are code

Array
(
    [available_price] => 770
    [category] => Fashion design & theory
    [mrp] => 770
    [source] => RediffBooks
    [title] => Shoes
    [url] => http://books.rediff.com/book/five-point-someone/9781851775378
)
Array
(
    [available_price] => 797
    [mrp] => 938
    [source] => URead-IN
    [title] => Shoes
    [url] => http://www.uread.com/search-books/9781851775378
)

... so on...

August Karlstrom
  • 10,773
  • 7
  • 38
  • 60

2 Answers2

0

You can use usort() to sort your array based on available_price:

$array = array(
   array(
     available_price => 770,
     category => "Fashion design & theory",
     mrp => 770,
     source => "RediffBooks",
     title => "Shoes",
     url => "http://books.rediff.com/book/five-point-someone/9781851775378"
   ),
   array(
     available_price => 797,
     mrp => 938,
     source => "URead-IN",
     title => "Shoes",
     url => "http://www.uread.com/search-books/9781851775378"
   ),
);

// sort the items in the array in ascending order based on available_price
usort($array, function($a, $b) {
   return $a['available_price'] > $b['available_price'];
});
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
  • 1
    `return $a['available_price'] < $b['available_price'];` should be `return $a['available_price'] - $b['available_price'];` – viral Jul 18 '15 at 08:32
-2

You should put all your arrays into one array and then loop then with bubble sort method OR use sort function with your defined compare function.

Daniel
  • 350
  • 4
  • 12
  • OP should in fact be using the "BakeSale" method of Array organisation, following the W3C official recommendations that array order is defined by flour and sugar concentrate in equal volumes. – Martin Jul 18 '15 at 08:26
  • Thank you so much for your reply, but still sorting not working. Following are my code. foreach($data2 as $data3){ print_r($data3['available_price']); usort($array, function($a, $b) { return $a['available_price'] > $b['available_price']; }); } but nothing result is displaying. where i am wrong ? – amit sharma Jul 20 '15 at 09:57