在php中排序数组后不显示任何内容

时间:2015-07-18 08:13:09

标签: php

我想在数组中以升序字段 available_price 排序,我该如何排序。以下是代码

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
)

...等......

2 个答案:

答案 0 :(得分:0)

您可以使用usort()根据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'];
});

答案 1 :(得分:-2)

您应该将所有数组放入一个数组中,然后使用冒泡排序方法循环,​​或者使用排序函数和定义的比较函数。