A piece of the $ tovar [' ATTRIBUTES '] code needs to be split into explode two times and assembled into my version. I was able to break it) but the problem cannot do it all with foreach

There is a string

Autopilot: No | Flight time: 8 min | Length: 166 mm | Width: 166 mm | Video camera: No

First break into | , and then on:

and collect so

Specs | Autopilot: | No Specs | Flight Time: | 8 min Specs | Length: | 166 mm Specs | Width: | 166 mm Specifications | Video Camera: | No

foreach($data as $a_product) { $tovar['_ATTRIBUTES_'] = $data[16]; } 

    1 answer 1

     <?php $res = []; $str = "Автопилот: НСт |ВрСмя ΠΏΠΎΠ»Ρ‘Ρ‚Π°: 8 ΠΌΠΈΠ½|Π”Π»ΠΈΠ½Π°: 166 ΠΌΠΌ|Π¨ΠΈΡ€ΠΈΠ½Π°: 166 ΠΌΠΌ|Π’ΠΈΠ΄Π΅ΠΎΠΊΠ°ΠΌΠ΅Ρ€Π°: НСт"; $arr1 = explode("|", $str); foreach ($arr1 as $i) { list($key, $value) = explode(": ", $i); $res[$key] = $value; } print_r($res) ?> 

    Output:

     Array ( [Автопилот] => НСт [ВрСмя ΠΏΠΎΠ»Ρ‘Ρ‚Π°] => 8 ΠΌΠΈΠ½ [Π”Π»ΠΈΠ½Π°] => 166 ΠΌΠΌ [Π¨ΠΈΡ€ΠΈΠ½Π°] => 166 ΠΌΠΌ [Π’ΠΈΠ΄Π΅ΠΎΠΊΠ°ΠΌΠ΅Ρ€Π°] => НСт ) 

    I hope I understood the problem correctly and it became easier to understand.

    PS To collect, just like in the question:

     foreach ($res as $key => $value) { printf("Π₯арактСристики|%s:|%s ", $key, $value); } 

    Output:

     Π₯арактСристики|Автопилот:|НСт Π₯арактСристики|ВрСмя ΠΏΠΎΠ»Ρ‘Ρ‚Π°:|8 ΠΌΠΈΠ½ Π₯арактСристики|Π”Π»ΠΈΠ½Π°:|166 ΠΌΠΌ Π₯арактСристики|Π¨ΠΈΡ€ΠΈΠ½Π°:|166 ΠΌΠΌ Π₯арактСристики|Π’ΠΈΠ΄Π΅ΠΎΠΊΠ°ΠΌΠ΅Ρ€Π°:|НСт 
    • And how to collect the best?) I do not want to become impudent, it’s really interesting how correct it is - Andry Lomakin
    • supplemented the answer. - ArcherGodson