Page 1 of 1

Array_multisort issue

Posted: Sat Nov 18, 2006 2:44 pm
by jeeep
I would like to rank an array in descending order (5 to 1) and I'm using "array_multisort" to sort them. However this method seems to be deleting one of my array items. For example:

Without array_multisort:

Code: Select all

<?php
                                                                
	$name[1] = "one";
	$kills[1] = "1111";
	$deaths[1] = "11";
	$ratio[1] = "1";

        $name[3] = "three";
	$kills[3] = "3333";
	$deaths[3] = "33";
	$ratio[3] = "3"; 


	$name[2] = "two";
	$kills[2] = "2222";
	$deaths[2] = "22";
	$ratio[2] = "2";
      
        $name[5] = "five";
	$kills[5] = "5555";
	$deaths[5] = "55";
	$ratio[5] = "5";
   
        $name[4] =  "four";
	$kills[4] = "4444";
	$deaths[4] = "44";
	$ratio[4] = "4";


  header("Content-type: image/png");
    $bg = 'backgrounds/BG10-G.png';
    $im = @ImageCreateFromPNG($bg);
    $font  = 2;
    $red = imagecolorallocate($im, 255, 0, 0);
    $grey = imagecolorallocate($im, 102, 102, 102);

$y = 120;

$name = array_diff($name, array(''));
for($i = 1; $i <= count($name); $i++)
{ 
	

        imagestring($im, $font, 30, $y ,   $i. "."  , $red);
	imagestring($im, $font, 50, $y ,  $name[$i] , $red); 
	imagestring($im, $font, 160, $y , $kills[$i], $red);  
	imagestring($im, $font, 220, $y , $deaths[$i], $red);  
	imagestring($im, $font, 280, $y , $ratio[$i], $red); 
        imageline($im, 40, $y, 300, $y , $grey);
	$y += 15;


}

      $path =  'xxxxxxxx.png';

imagepng($im, $path);
imagedestroy($im);
      ?>
Gives me:
1. one 1111 11 1
2. two 2222 22 2
3. three 3333 33 3
4. four 4444 44 4
5. five 5555 55 5

With the array_multisort:

Code: Select all

<?php
                                                                
	$name[1] = "one";
	$kills[1] = "1111";
	$deaths[1] = "11";
	$ratio[1] = "1";

        $name[3] = "three";
	$kills[3] = "3333";
	$deaths[3] = "33";
	$ratio[3] = "3"; 


	$name[2] = "two";
	$kills[2] = "2222";
	$deaths[2] = "22";
	$ratio[2] = "2";
      
        $name[5] = "five";
	$kills[5] = "5555";
	$deaths[5] = "55";
	$ratio[5] = "5";
   
        $name[4] =  "four";
	$kills[4] = "4444";
	$deaths[4] = "44";
	$ratio[4] = "4";


  header("Content-type: image/png");
    $bg = 'backgrounds/BG10-G.png';
    $im = @ImageCreateFromPNG($bg);
    $font  = 2;
    $red = imagecolorallocate($im, 255, 0, 0);
    $grey = imagecolorallocate($im, 102, 102, 102);


$y = 120;

array_multisort($ratio, SORT_DESC, $name, SORT_DESC, $kills, SORT_DESC, $deaths, SORT_DESC);

$name = array_diff($name, array(''));
for($i = 1; $i <= count($name); $i++)
{ 
	

        imagestring($im, $font, 30, $y ,   $i. "."  , $red);
	imagestring($im, $font, 50, $y ,  $name[$i] , $red); 
	imagestring($im, $font, 160, $y , $kills[$i], $red);  
	imagestring($im, $font, 220, $y , $deaths[$i], $red);  
	imagestring($im, $font, 280, $y , $ratio[$i], $red); 
        imageline($im, 40, $y, 300, $y , $grey);
	$y += 15;


}

      $path =  'xxxxxxxx.png';

imagepng($im, $path);
imagedestroy($im);
      ?>
Gives me:
1. four 4444 44 4
2. three 3333 33 3
3. two 2222 22 2
4. one 1111 11 1
5.

What is the array_multisort doing to effect the $name variable to make it not show the 5th name?