【问题标题】:PHP Sort pictures by biggest width and where width is bigger than heightPHP按最大宽度和宽度大于高度的地方对图片进行排序
【发布时间】:2016-01-31 03:20:22
【问题描述】:

我有一个这样的数组:

 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 可以吗?

【问题讨论】:

    标签: php height width usort


    【解决方案1】:

    试试这个:

    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;
    });
    

    【讨论】:

      猜你喜欢
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 2017-12-08
      • 2014-04-04
      • 2013-05-23
      • 1970-01-01
      相关资源
      最近更新 更多