I have this multidimensional array:
$value = [
'name' => ['silverado', 'civic'],
'type' => ['truck', 'car'],
];
I want to basically sort both of these child arrays by the values of name
, ascending.
I have this code, which works:
$value = [
'name' => ['silverado', 'civic'],
'type' => ['truck', 'car'],
];
$name_type = [];
$columns = [];
foreach ($value['name'] as $k => $v) {
$name_type[$v] = $value['type'][$k];
}
ksort($name_type);
foreach ($name_type as $name => $type) {
$columns['name'][] = $name;
$columns['type'][] = $type;
}
$value = $columns;
I'm just curious if there is a better way of coding this rather than using 2 foreach loops.