views:

173

answers:

2

Hi, new here so thanks for taking the time to read my question.

I am running some PHP code that compares the numbers enter on the screen with those in a database. The problem I am having is ordering the two dimensional array after manipulating each line. It looks as though the array id numbers are being removed. I would like to order the array by column [2] in descending order. Can anyone offer any help?

while( $a_row = mysql_fetch_array( $result))
    {
    $draw = array($a_row['Drawn1'], 
  $a_row['Drawn2'], 
  $a_row['Drawn3'], 
  $a_row['Drawn4'], 
  $a_row['Drawn5'], 
  $a_row['Drawn6'], 
  $a_row['Drawn7'], 
  $a_row['Drawn8']);
    $numbers = array("6", "9", "4", "8", "14", "18");
    if (count(array_intersect($draw, $numbers)) >= 1) {
     $rs = array(($a_row['DrawNo']), join(" , ",array_intersect($draw, $numbers)), count(array_intersect($draw, $numbers)));
    } else {
     $rs = null; 
    }
    array_multisort($rs[1], SORT_NUMERIC, SORT_DESC, $rs[0], SORT_ASC, SORT_STRING);
    print_r ($rs);
    echo  "<br />";
    }

This is what the output looks like.

Array ( [0] => A0048 [1] => 14 [2] => 1 )
Array ( [0] => A0049 [1] => 6 , 14 , 8 , 18 [2] => 4 )
Array ( [0] => A0050 [1] => 14 [2] => 1 )
Array ( [0] => A0051 [1] => 14 [2] => 1 )
Array ( [0] => A0052 [1] => 18 [2] => 1 )
Array ( [0] => A0053 [1] => 6 , 14 [2] => 2 )
Array ( [0] => A0054 [1] => 6 [2] => 1 )
Array ( [0] => A0055 [1] => 14 [2] => 1 )
Array ( [0] => A0056 [1] => 4 [2] => 1 )
Array ( [0] => A0057 [1] => 9 , 6 , 4 [2] => 3 )

Thanks for your time
zeroanarchy

A: 

you are probably looking for: http://php.net/manual/en/function.array-multisort.php

dusoft
Hi dusoft, thanks for taking the time to look at my question. I am currently using the array_multisort function in the above code and it is not returning the desired result (see output). If I try and order the two dimensional array by column[2].Desired Output:Array ( [0] => A0049 [1] => 6 , 14 , 8 , 18 [2] => 4 ) Array ( [0] => A0057 [1] => 9 , 6 , 4 [2] => 3 ) Array ( [0] => A0053 [1] => 6 , 14 [2] => 2 ) Array ( [0] => A0050 [1] => 14 [2] => 1 )
zeroanarchy
+1  A: 

Numeric array keys being reindexed is part of the documented behavior of array_multisort(). If you need keys preserved, you need to convert them to string keys.

chaos