ggoran
ggoran

Reputation: 620

Php multidimensional array [matrix] sorting columns

If I have a matrix

[3,1,2,4]

[a,b,c,d]

And I need sort first row with usort key. But when I want reorder first array how do column moves at well

So output will be like this in this case described top

[1,2,3,4]

[b,c,a,d]

Upvotes: 0

Views: 245

Answers (2)

Felippe Duarte
Felippe Duarte

Reputation: 15131

You can use array_multisort:

$x = [[3,1,2,4],['a','b','c','d']];
array_multisort($x[0], $x[1]);

var_dump($x);

Output:

array(2) {
  [0]=>
  array(4) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
    [3]=>
    int(4)
  }
  [1]=>
  array(4) {
    [0]=>
    string(1) "b"
    [1]=>
    string(1) "c"
    [2]=>
    string(1) "a"
    [3]=>
    string(1) "d"
  }
}

Upvotes: 4

Sutton
Sutton

Reputation: 310

I think what you're looking for is ksort.

Upvotes: 0

Related Questions