У меня есть массив, который выглядит как
$array = [
//...
'name' => ['value' => 'Raj KB'],
'street' => ['value' => 'Street ABC'],
'city' => ['value' => 'Dubai'],
'country_id' => ['value' => 'UAE'],
'region' => ['value' => 'DXB'],
'region_id' => ['value' => 11],
'zip_code' => ['value' => 12345],
'city_id' => ['value' => 22],
//...
];
Я хотел бы отсортировать массив так, чтобы возникали ключи country_id
, region
, region_id
, city
, city_id
последовательно, сохраняя позицию других.
Ожидаемый результат
$array = [
//...
'name' => ['value' => 'Raj KB'],
'street' => ['value' => 'Street ABC'],
'country_id' => ['value' => 'UAE'],
'region' => ['value' => 'DXB'],
'region_id' => ['value' => 11],
'city' => ['value' => 'Dubai'],
'city_id' => ['value' => 22],
'zip_code' => ['value' => 12345],
//...
];
Я пробовал как:
Проба № 1
uksort($array, function ($a, $b) {
$order = ['country_id' => 0, 'region' => 1, 'region_id' => 2, 'city' => 3, 'city_id' => 4];
if (isset($order[$a]) && isset($order[$b])) {
return $order[$a] - $order[$b];
} else {
return 0;
}
});
var_dump($array);
Проба # 2
uksort($array, function ($a, $b) {
$order = ['country_id' => 0, 'region' => 1, 'region_id' => 2, 'city' => 3, 'city_id' => 4];
if (!isset($order[$a]) && !isset($order[$b])) {
return 0;
} elseif (!isset($order[$a])) {
return 1;
} elseif (!isset($order[$b])) {
return -1;
} else {
return $order[$a] - $order[$b];
}
});
var_dump($array);
Но остальные заказы больше не обслуживаются. Поэтому я хочу, чтобы эти настраиваемые поля отображались в том же порядке, не нарушая позиций других. Например, сначала должен появиться name
, et c.