Hello, I have nested arrays:

Array ( [0] => Array ( [id] => 1 [title_ru] => Сиделка [description_ru] => Уход за пожилыми людьми, помощь больным и не самостоятельным [meta_desc_ru] => Уход за пожилыми людьми, помощь больным и не самостоятельным [meta_key_ru] => [title_en] => Nurse [description_en] => Care for the elderly, the sick and not an independent [meta_desc_en] => Care for the elderly, the sick and not an independent [meta_key_en] => [title_it] => Badante [description_it] => La cura per gli anziani, i malati e non indipendente [meta_desc_it] => La cura per gli anziani, i malati e non indipendente [meta_key_it] => ) [1] => Array ( [id] => 2 [title_ru] => Уборки [description_ru] => Уборки домов, уборка квартир и офисов, уборка в подъезде [meta_desc_ru] => Уборки домов, уборка квартир и офисов, уборка в подъезде [meta_key_ru] => Уборки домов, уборка квартир и офисов, уборка в подъезде [title_en] => Cleaning [description_en] => Cleaning houses, cleaning offices and apartments, cleaning in the stairwell [meta_desc_en] => Cleaning houses, cleaning offices and apartments, cleaning in the stairwell [meta_key_en] => Cleaning houses, cleaning offices and apartments, cleaning in the stairwell [title_it] => Pulizia [description_it] => Pulizia di uffici e appartamenti di pulizia, la pulizia nella tromba delle scale [meta_desc_it] => Pulizia di uffici e appartamenti di pulizia, la pulizia nella tromba delle scale [meta_key_it] => Pulizia di uffici e appartamenti di pulizia, la pulizia nella tromba delle scale ) [2] => Array ( [id] => 3 [title_ru] => Недвижимость [description_ru] => Объявления по недвижимости, продажа квартир, съем или сдача квартир, комнат. Тут, вы быстро найдете себе крышу над головой! [meta_desc_ru] => Объявления по недвижимости, продажа квартир, съем или сдача квартир, комнат. Тут, вы быстро найдете себе крышу над головой! [meta_key_ru] => продажа квартир, съем или сдача квартир, съем или сдача комнаты [title_en] => The property [description_en] => Classifieds real estate, apartments for sale, renting or renting apartments, rooms. Here, you will quickly find yourself a roof over his head! [meta_desc_en] => Classifieds real estate, apartments for sale, renting or renting apartments, rooms. Here, you will quickly find yourself a roof over his head! [meta_key_en] => apartments for sale, renting or renting apartments [title_it] => Immobili [description_it] => Annunci immobiliari, appartamenti in vendita, noleggio o affitto di appartamenti, camere. Qui, si rapidamente trovare te stesso un tetto sopra la testa! [meta_desc_it] => Annunci immobiliari, appartamenti in vendita, noleggio o affitto di appartamenti, camere. Qui, si rapidamente trovare te stesso un tetto sopra la testa! [meta_key_it] => appartamenti in vendita, noleggio o affitto di appartamenti, camere ) ) 

but I want to overwrite it so that it is like this:

 Array ( [0] => Array ( [id] => 1 [title_ru] => Сиделка [description_ru] => Уход за пожилыми людьми, помощь больным и не самостоятельным [meta_desc_ru] => Уход за пожилыми людьми, помощь больным и не самостоятельным [meta_key_ru] => ) [1] => Array ( [id] => 2 [title_ru] => Уборки [description_ru] => Уборки домов, уборка квартир и офисов, уборка в подъезде [meta_desc_ru] => Уборки домов, уборка квартир и офисов, уборка в подъезде [meta_key_ru] => Уборки домов, уборка квартир и офисов, уборка в подъезде ) [2] => Array ( [id] => 3 [title_ru] => Недвижимость [description_ru] => Объявления по недвижимости, продажа квартир, съем или сдача квартир, комнат. Тут, вы быстро найдете себе крышу над головой! [meta_desc_ru] => Объявления по недвижимости, продажа квартир, съем или сдача квартир, комнат. Тут, вы быстро найдете себе крышу над головой! [meta_key_ru] => продажа квартир, съем или сдача квартир, съем или сдача комнаты ) ) 

I can not understand how, because there are 3 arrays, I do not understand how to make a cycle correctly

  • you have not three arrays here, but nested arrays. - teran

3 answers 3

You can use a simple loop through the elements of the array, and the results should be added to the new array ( $data is your original array)

 $result = []; foreach($data as $d){ $result[] = [ 'id' => $d['id'], 'title_ru' => $d['title_ru'], 'description_ru' => $d['description_ru'], 'meta_desc_ru' => $d['meta_desc_ru'], 'meta_key_ru' => $d['meta_key_ru'] ]; } 

You can also use the function array_map()

 $result = array_map(function($d){ return [ 'id' => $d['id'], 'title_ru' => $d['title_ru'], 'description_ru' => $d['description_ru'], 'meta_desc_ru' => $d['meta_desc_ru'], 'meta_key_ru' => $d['meta_key_ru'] ]; }, $data); 

It is also possible to approach when you simply walk through the source array, as well as its nested element, and remove the unwanted with the help of unset() , as well as other variations.

  • And if the keys ending in _ru will be not three but thirty? :) - Dmitriy Simushev February
  • one
    @DmitriySimushev when it is, then ask :) to your answer, in general, the same kind of question you can ask - teran
  • The locale is a separate entity, your regulars are not needed here, you need to revise the data structure. - vp_arth
  • H. teran thanks for the tip! - privetsh

For example:

 $original = [/* ... */]; $filtered = []; foreach ($original as $block_key => $block) { $filtered[$block_key] = array_filter($block, function ($key) { return ($key === 'id') || preg_match('/_ru$/', $key); }, ARRAY_FILTER_USE_KEY); } var_dump($filtered); 

And here is a working example on IDEOne.

    Thanks to everyone who responded while waiting for an answer, I went outside to refresh the boiling brain. When I came back and read your answers, and then a special thank you to teran, he would push me on the option that fully approached me!

      foreach($row as $key => $value) { $result[$key] = array( 'id' => $value['id'], 'title' => $value['title_'.$_SESSION['lang']], 'description' => $value['description_'.$_SESSION['lang']], 'meta_desc' => $value['meta_desc_'.$_SESSION['lang']], 'meta_key' => $value['meta_key_'.$_SESSION['lang']] ); }