I have 2 arrays, both are multidimensional with same number of elements and same values, which are on different positions (those values are actually ID-s from my database, so one ID appears only once). How can I sort second array with values which are in first array?

For example - if first array looks like:

```
$array1[0][0] = 1;
$array1[0][x] = it doesn't matter what's here
$array1[1][0] = 4;
$array1[1][x] = it doesn't matter what's here
$array1[2][0] = 3;
$array1[2][x] = it doesn't matter what's here
...

```

how to sort second array so it would have same values as array1 on indexes [0][0], [1][0], [2][0], etc.

How I could solve problem is:

```
$i=0
while ($i < (count($array1)-2)){ // * check down

  $find_id = $array1[$i][0];

  // here I need to search for index of that ID in other array
  $position = give_index($find_id, $array2);

  // swapping positions
  $temp = array2[$i][0];
  $array2[$i][0] = $array2[$position][0];
  $array2[$position][0] = $temp;

  // increasing counter
  i++;
}

function give_index($needle, $haystack){
  for ($j = 0, $l = count($haystack); $j < $l; ++$j) {
        if (in_array($needle, $haystack[$j][0])) return $j;
  }
  return false;
}

```

* \*There is only -2 because indexes start from 0 and also for the last element you don't need to check since it would be automatically sorted by last iteration of while-loop.

I don't find this solution good as I think that this is quite simple issue (maybe it's not even correct). Is there easier way in PHP that I'm missing?