简体   繁体   中英

PHP | Order pictures array by rectangular

I have an array like this one:

 array(
    0 => array(
       width => "213",
       height => "50"
    ),
    1 => array(
       width => "120",
       height => "204"
    )
 ) etc...

Now I want to order this array by the biggest rectangular in the length for example in this array it is the number with width=>"213" and height=>"50"

My try to do this was that, using usort() :

usort($images, function($a, $b) { 
    return $b['height'] - $a['width'];
});

But it sort's the pictures just for their size. Anybody a idea?

Assuming length as the width of the image -

usort($images, function($a, $b) { 
    return $b['width'] - $a['width'];
});

And if you mean by area of the rectangular image -

usort($images, function($a, $b) { 
    return ($b['height'] * $b['width']) - ($a['width'] * $a['height']);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM