Build an array of parameters for array_multisort(). Start with the highest priority rows and if there are multiple priorities, work your way down. Ascending sorting directions don't need to be mentioned, but for a descending sort, push SORT_DESC as a parameter. After the sorting parameters, push references to the all of the original associatively-keyed rows.
To sort by the first row in an ascending direction: Demo
$params = [];
foreach ($array as &$params[]);
array_multisort(...$params);
var_export($array);
Or to sort by order in a descending direction: Demo
$array = [
'sortBy' => [3, 2, 1],
'other' => ['x', 'y', 'z'],
'xxx' => [3, 2, 1],
'foo' => [3, 2, 1],
'bar' => ['c', 'b', 'a'],
];
$params[] = $array['other'];
$params[] = SORT_DESC;
foreach ($array as &$params[]);
array_multisort(...$params);
var_export($array);
Output:
array (
'sortBy' =>
array (
0 => 1,
1 => 2,
2 => 3,
),
'other' =>
array (
0 => 'z',
1 => 'y',
2 => 'x',
),
'xxx' =>
array (
0 => 1,
1 => 2,
2 => 3,
),
'foo' =>
array (
0 => 1,
1 => 2,
2 => 3,
),
'bar' =>
array (
0 => 'a',
1 => 'b',
2 => 'c',
),
)
Note that there are any ties to break, the following array parameter will be used.
For a stable sort which will ONLY mutate the array by other sorting, preserve the keys while sorting the other row, then loop over all of the rows and use the new index orders as the basis for all values. array_replace() will see that the values are written to the correct keys. Demo
arsort($array['other']);
var_export(
array_map(
fn($row) => array_replace(
$array['other'],
$row
),
$array
)
);
Same output as the previous other-sorting script.