@Lavrov95

Is there any more easy and short version for array sorting?

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 Сюник регион)",
    ];
  • Вопрос задан
  • 66 просмотров
Решения вопроса 1
nokimaro
@nokimaro
Меня невозможно остановить, если я смогу начать.
//add any lang => option_* pairs here
$langs = [
    'general' => 'option_title',
    'en' => 'option_title_en',
    'ru' => 'option_title_ru',
    'am' => 'option_title_am',
];

$placesSelectizeOptionsArray = [];
foreach($places as $place)
{
    $option = [];
    foreach($langs as $lang => $title_key)
    {
        //has parent_name_*
        if(isset($place['option_parent_name_'.$lang]))
        {
            $option[$title_key] = 
                    $place['option_name_'.$lang] . 
                    ' (' . $place['option_type_'.$lang] . 
                    $this->getTranslator()->translate(' of ') . 
                    $place['option_parent_name_'.$lang] . 
                    ' ' . $place['option_parent_type_'.$lang] . ')';
        }
        elseif(isset($place['option_name_'.$lang]))
        {
            $option[$title_key] = 
                     $place['option_name_'.$lang] . 
                    ' (' . $place['option_type_'.$lang] . ')';
        }
    }
    
    $placesSelectizeOptionsArray[] = $option;
}
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
ThunderCat
@ThunderCat Куратор тега PHP
{PHP, MySql, HTML, JS, CSS} developer
It's not about sorting, in your case is kind of filtering, so array_filter() must be used instead of this code. Or array_map() if u need all values modify and return.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы