1

I have a multidimensional array, and I need to sort that array by a specific key in that array.

I add to the array like this in a for loop

        $myArr[$i][0] = $row[1];

        $myArr[$i][1] = $row[2];

        $myArr[$i][2] = $row[3];

Now lets say that the value of $row[3] is DATE_ATOM.

How can i arrange the completed array by $myArr[$i][2]?

Thanks!

Graeme Leighfield
  • 2,825
  • 3
  • 23
  • 38

1 Answers1

2

What you are probably looking for is array_multisort(), specifically this example usage (Sorting database results).

For example (based on your code above):

$i = 0;
$myArr = $col1 = $col2 = $col3 = array();
foreach ($rows as $row) {
  $myArr[$i][0] = $col1[$i] = $row[1];
  $myArr[$i][1] = $col2[$i] = $row[2];
  $myArr[$i][2] = $col3[$i] = $row[3];
  $i++;
}

array_multisort($col3, SORT_ASC, $myArr);
var_dump($myArr);
DaveRandom
  • 87,921
  • 11
  • 154
  • 174