PHP 按最大宽度和宽度大于高度对图片进行排序

标签 php height width usort

我有一个像这样的数组:

 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/

相关文章:

php - 错误的时间戳/时区转换

php - 显示 jquery ajax 执行状态

html - 在所有浏览器中设置包装器高度相等

ios - iPhone 屏幕键盘的高度是多少?

php - 从php中的ttf-font获取单个字符的宽度?

选择的 CSS 宽度百分比比其他标签短

javascript - 从 $_POST PHP 获取值(value)

javascript - CSS:为什么照片没有缩放?

html - CSS:制作一个DIV全屏宽高不改变里面绝对div的比例

php - 如果 id 存在于另一个表中则更新表数据