PHP - 按高度排序图像数组[重复]

This question already has an answer here:

Taking the following;

// Looping through images passed to function...

list($width[$i], $height[$i]) = getimagesize($img_urls[$i]);

// ... Now to reorder by height

How can I reorder the $height array to tallest > shortest, while maintaining the key relationship with the correct $width value?

I've attempted with uasort, but I've had no luck. The "closest" attempt I've had is below, but that sorts from smallest to largest

uasort($height, function($a, $b) use ($i) {
    return $a[$i] + $b[$i];
});
</div>

Firstly, use structures:

class Image {
    public $width;
    public $height;
}

This is neccessary because width and heights of the image are hardly connected. Width of one image shows nothing without height of image. Same for height. You should link this data. For example, in structure.

After that, get images heights and widths:

$images = array();

// get widths and heights
loop start
    $img = new Image();
    $img->width = assign width;
    $img->height = assign height;
    $images[] = $img;
loop end

Finally, sort:

function cmp($a, $b) {
    if ($a->height == $b->height) {
        return 0;
    }
    return ($a->height < $b->height) ? -1 : 1;
}

uasort($images, 'cmp');

//$images are sorted by height now

Let me know if something is unclear.

uasort should work fine:

uasort( $height, function ( $a, $b ) { if ( $a == 0 ) { return 0; } else { return ( $a > $b ) ? -1 : 1; } } );

If I do this:

$arr = array();
$arr[0] = 512;
$arr[1] = 1024;
uasort( $arr, function ( $a, $b ) { if ( $a == 0 ) { return 0; } else { return ( $a > $b ) ? -1 : 1; } } );
var_dump( $arr );
var_dump( $arr[0] );
var_dump( $arr[1] );

I get:

array(2) {
  [1]=>
  int(1024)
  [0]=>
  int(512)
}
int(512)
int(1024)

Try this:

$images = array();
for ($i = 0; $i < count($img_urls); $i++) {
    $image_size = getimagesize($img_urls[$i]);
    $images[$image_size[1]]['width'] = $image_size[0];
    $images[$image_size[1]]['height'] = $image_size[1];
}
sort($images);
var_dump($images);