Is there any more easy and short version for array sorting?
$places
is
$places[0] = [
"option_name_en" => "Abovyan",
"option_type_en" => "city",
"option_name_general" => "Աբովյան",
"option_type_general" => "քաղաք",
"option_name_ru" => "Абовян",
"option_type_ru" => "город",
"option_value" => "50",
"option_parent_name_en" => "Kotayk",
"option_parent_type_en" => "region",
"option_parent_name_general" => "Կոտայք",
"option_parent_type_general" => "մարզ",
"option_parent_name_ru" => "Котайк",
"option_parent_type_ru" => "регион",
];
$places[1] = [
"option_name_en" => "Agarak",
"option_type_en" => "city",
"option_name_general" => "Ագարակ",
"option_type_general" => "քաղաք",
"option_name_ru" => "Агарак",
"option_type_ru" => "город",
"option_value" => "57",
"option_parent_name_en" => "Syunik",
"option_parent_type_en" => "region",
"option_parent_name_general" => "Սյունիք",
"option_parent_type_general" => "մարզ",
"option_parent_name_ru" => "Сюник",
"option_parent_type_ru" => "регион",
];
I am doing like this but I need more easy version of array sorting because there can be more
languages.
foreach ($places as $place) {
$placesArray['option_value'] = $place['option_value'];
if (!is_null($place['option_parent_name_general'])) {
foreach ($place as $key => $value) {
if ($key === 'option_name_en' ) {
$placesArray['option_title_en'] = $place['option_name_en'] . ' (' . $place['option_type_en'] . $this->getTranslator()->translate(' of ') . $place['option_parent_name_en'] . ' ' . $place['option_parent_type_en'] . ')';
}
if ($key === 'option_name_ru' ) {
$placesArray['option_title_ru'] = $place['option_name_ru'] . ' (' . $place['option_type_ru'] . $this->getTranslator()->translate(' of ') . $place['option_parent_name_ru'] . ' ' . $place['option_parent_type_ru'] . ')';
}
if ($key === 'option_name_am' ) {
$placesArray['option_title_am'] = $place['option_name_am'] . ' (' . $place['option_type_am'] . $this->getTranslator()->translate(' of ') . $place['option_parent_name_am'] . ' ' . $place['option_parent_type_am'] . ')';
}
if ($key === 'option_name_general' ) {
$placesArray['option_title'] = $place['option_name_general'] . ' (' . $place['option_type_general'] . $this->getTranslator()->translate(' of ') . $place['option_parent_name_general'] . ' ' . $place['option_parent_type_general'] . ')';
}
}
$placesSelectizeOptionsArray[] = $placesArray;
} else {
foreach ($place as $key => $value) {
if ($key === 'option_name_en') {
$placesArray['option_title_en'] = $place['option_name_en'] . ' (' . $place['option_type_en'] . ')';
}
if ($key === 'option_name_ru') {
$placesArray['option_title_ru'] = $place['option_name_ru'] . ' (' . $place['option_type_ru'] . ')';
}
if ($key === 'option_name_am') {
$placesArray['option_title_am'] = $place['option_name_am'] . ' (' . $place['option_type_am'] . ')';
}
if ($key === 'option_name_general') {
$placesArray['option_title'] = $place['option_name_general'] . ' (' . $place['option_type_general'] . ')';
}
}
$placesSelectizeOptionsArray[] = $placesArray;
}
}
Resalt of
var_dump($placesSelectizeOptionsArray)
must be
$placesSelectizeOptionsArray[0] = [
"option_value" => "50",
"option_title_en" => "Abovyan (city of Kotayk region)",
"option_title" => "Աբովյան (քաղաք of Կոտայք մարզ)",
"option_title_ru" => "Абовян (город of Котайк регион)",
];
$placesSelectizeOptionsArray[1] => array:4 [
"option_value" => "57"
"option_title_en" => "Agarak (city of Syunik region)",
"option_title" => "Ագարակ (քաղաք of Սյունիք մարզ)",
"option_title_ru" => "Агарак (город of Сюник регион)",
];