我有一个像这样的数组:
array(
0 => array(
width => "213",
height => "50"
),
1 => array(
width => "120",
height => "204"
)
) etc...
现在我想通过宽度值最大且宽度大于高度的图片来订购此数组。
我尝试这样做,使用usort()
:
usort($results['bossresponse']['images']['results'], function($a, $b) {
return $b['width'] - $a['width'];
});
但它的图片只是因为它们的宽度,而不是图片的宽度大于高度。是否可以使用usort?
答案 0 :(得分:2)
试试这个:
usort($results['bossresponse']['images']['results'], function($a, $b) {
//Case if A has bigger width than height but B doesn't
if($a["width"]>$a["height"] && !($b["width"]>$b["height"])) return -1;
//Case if B has bigger width than height but A doesn't
if(!($a["width"]>$a["height"]) && $b["width"]>$b["height"]) return 1;
//They both/neither have bigger width than height, so compare widths
if($a["width"]>$b["width"]) return -1;
elseif($b["width"]>$a["width"]) return 1;
else return 0;
});