我有一个像这样的数组:
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可以吗?
最佳答案
试试这个:
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;
});
关于PHP 按最大宽度和宽度大于高度对图片进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33440395/