Hello all. I am trying to sort a multidimensional array based on 2 criteria. This is a new one for me, so forgive me if it's something simple.
I have a 2D array with 3 indices. 0 is the y coordinate, 1 is x, and 2 is reserved for something else.
I need to sort on indice 1, then 0. I need to preserve the first sorts place.
i.e.
[9] => Array
(
[0] => 625
[1] => 1410
[2] => 64_2000_BMW_740_c.jpg
)
[8] => Array
(
[0] => 625
[1] => 1502
[2] => 69_2000_BMW_740_k.jpg
)
[10] => Array
(
[0] => 795
[1] => 1502
[2] => 70_2000_BMW_740_i.jpg
)
Here is the code I am using from the web:
function compare($x, $y){
if ( $x[1] == $y[1] )
return 0;
else if ( $x[1] < $y[1] )
return -1;
else
return 1;
}
/** get images array (in "y x" format **/
$images = $_POST['images'];
/** explode images array into y x cords **/
foreach($images as $key => $value){
$images[$key] = explode("\x20", $value);
/** put image name into 3rd element **/
$images[$key][] = $listingImages[$key];
}
/** sort values by x, then y **/
/** x = $images[1] & y = $images[0] **/
usort($images, 'compare');
echo '<pre>'; print_r($images); echo '</pre>';
Obvisouly, this sorts based on X and nothing else.
Any help would be appreciated.