Php array sort

how to sort this array using nums ...

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

      

... so I get this one?

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

      

+2


a source to share


2 answers


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



+1


a source


Try sort function.

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

      

This function sorts the array. Items will be ranked from lowest to highest when this feature is completed.

Also check asort

andarsort



EDIT

I didn't take your multidimensional array into account.

   <?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;
      }
    ?>

      

+1


a source







All Articles