tags:

views:

56

answers:

2

how do i sort this array by the nums...

Array(
  [nums] => Array
   (
    [0] => 34
    [1] => 12
    [2] => 13
   )
  [players] => Array
   (
    [0] => Mike
    [1] => Bob
    [2] => Mary
   )
)

... so that i get this one?

Array(
  [nums] => Array
   (
    [0] => 12
    [1] => 13
    [2] => 34
   )
  [players] => Array
   (
    [0] => Bob
    [1] => Mary
    [2] => Mike
   )
)
+1  A: 

Try the sort function.

bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.

Also check out asort and arsort

EDIT

I did not take into account your Multidimensional array.

   <?php
    //code derived from comments on the php.net/sort page.
    // $sort used as variable function--can be natcasesort, for example
      function sort2d( &$arrIn, $index = null, $sort = 'sort') {
        // pseudo-secure--never allow user input into $sort
        if (strpos($sort, 'sort') === false) {$sort = 'sort';}
        $arrTemp = Array();
        $arrOut = Array();

        foreach ( $arrIn as $key=>$value ) {
          reset($value);
          $arrTemp[$key] = is_null($index) ? current($value) : $value[$index];
        }

        $sort($arrTemp);

        foreach ( $arrTemp as $key=>$value ) {
          $arrOut[$key] = $arrIn[$key];
        }

        $arrIn = $arrOut;
      }
    ?>
Russell Dias
+1  A: 

array_multisort($x['nums'],$x['players']);

stillstanding
looks like it works.