我正在尝试按英里排序数组。
$i = 0;
foreach($results as $key => $value)
{
echo $results['miles'][$i] . "<br />";
echo $results['postcode'][$i] . "<br />";
echo $results['id'][$i] . "<br />";
echo $results['description'][$i] . "<br />";
echo $results['title'][$i] . "<br />";
echo $results['thumbnail'][$i] . "<br />";
$i++;
}
你可以看到我在这里有6个不同的按键。我想按照ASCENDING的顺序按$ results ['miles']订购所有东西。
这是我的数组结构:
Array ( [miles] => Array ( [0] => 0 [1] => 0 [2] => 14 [3] => 8 [4] => 8 [5] => 0 [6] => 8 [7] => 14 ) [title] => Array ( [0] => Stunning 1 Bedroom Apartment With Concierge [1] => Big Double Inc Bills+ Balcony+Free Parking [2] => Large, Sunny Flat In Willesden [3] => Brewhouse Yard EC1V [4] => Stunning 2 Double Bed Flat - City [5] => Room To Let £575 Pm Bills Included [6] => All Bills Inclusive | Gorgeous 1 Bed In The City [7] => Large Double Room In Zone 2 (Kensal Green 2 Mins) ) [id] => Array ( [0] => 187 [1] => 176 [2] => 186 [3] => 178 [4] => 179 [5] => 177 [6] => 183 [7] => 182 ) [thumbnail] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => ) [postcode] => Array ( [0] => IG11 [1] => ig11 [2] => NW10 [3] => EC1 [4] => ec1 [5] => ig11 [6] => ec1 [7] => NW10 ) )
任何帮助将不胜感激!
答案 0 :(得分:0)
如果可以,最好的方法是在数据进入脚本之前对数据进行排序(如果它来自SQL然后在那里订购)。
如果您需要在PHP中执行此操作,但可以使用uasort
函数,它允许您定义自己的比较:
function compareMiles($a, $b) {
if ($a['miles'] == $b['miles']) {
return 0;
}
return ($a['miles'] < $b['miles']) ? -1 : 1;
}
uasort($results, 'compareMiles');
答案 1 :(得分:0)
您可以使用usort()
功能对其进行排序。
答案 2 :(得分:0)