I am trying to sort a multidimensional array by multiple keys without affecting previously sorted array's index. I can only try to sort the first time using the name
key. I am not sure how to sort again the array with the next key without affecting the order of previously sorted array.
My array looks like this:
$columns = ['model', 'color'];
$data = [
[
'name' => 'Joe Smith',
'model' => 'Honda',
'color' => 'yellow'
],
[
'name' => 'Joe Smith',
'model' => 'Honda',
'color' => 'red'
],
[
'name' => 'Joe Smith',
'model' => 'Toyota',
'color' => 'red'
],
[
'name' => 'Joe Smith',
'model' => 'Tesla',
'color' => 'orange'
],
[
'name' => 'Joe Smith',
'model' => 'Tesla',
'color' => 'aqua'
],
[
'name' => 'Joe Smith',
'model' => 'Volkswagen',
'color' => 'teal'
],
[
'name' => 'Joe Smith',
'model' => 'Honda',
'color' => 'blue'
],
];
foreach($data as $value) {
$row = [];
foreach($columns as $column) {
$row[] = $value[$column];
}
$sheet[] = $row;
}