There is a line of the form

3199996|Amazing Dubbing||3199114|AniDUB||3204688|AniLibria||3204934|AniMaunt||3198662|AnimeVost||3199009|AniPlay||3199879|AniRise||3198674|AniStar||3210309|Anything Group||3199048|KBK||3201994|KitsuneBox||3200688|Risens Team||3199154|SHIZA Project||3199180|TAKEOVER Project|||3206565|AniDUB||3214108|AniLibria||3204770|AnimeVost||3214571|AniRise||3204751|AniStar||3213831|Anything Group||3206322|KBK||3211200|KitsuneBox||3205028|Risens Team||3210273|SHIZA Project|||3216986|AniDUB||3218426|AniLibria||3214254|AnimeVost||3219614|AniRise||3214057|AniStar||3214746|Anything Group||3219664|KBK||3214608|Risens Team||3215849|SHIZA Project|||3218870|AnimeVost||3218862|AniStar||3219909|Anything Group||3218760|Risens Team 

All i can do with this porridge

  $str = "3199996|Amazing Dubbing||3199114|AniDUB||3204688|AniLibria||3204934|AniMaunt||3198662|AnimeVost||3199009|AniPlay||3199879|AniRise||3198674|AniStar||3210309|Anything Group||3199048|KBK||3201994|KitsuneBox||3200688|Risens Team||3199154|SHIZA Project||3199180|TAKEOVER Project|||3206565|AniDUB||3214108|AniLibria||3204770|AnimeVost||3214571|AniRise||3204751|AniStar||3213831|Anything Group||3206322|KBK||3211200|KitsuneBox||3205028|Risens Team||3210273|SHIZA Project|||3216986|AniDUB||3218426|AniLibria||3214254|AnimeVost||3219614|AniRise||3214057|AniStar||3214746|Anything Group||3219664|KBK||3214608|Risens Team||3215849|SHIZA Project|||3218870|AnimeVost||3218862|AniStar||3219909|Anything Group||3218760|Risens Team"; $mas = explode("|||", $str); $str_result=""; foreach ($mas as $value) { $mas1 = explode("||", $value); foreach ($mas1 as $value1) { $mas2 = explode("|", $value1); $str_result .= $mas2[1]."-".$mas2[0]."<br>"; } } echo $str_result; 

Result of code execution

 Amazing Dubbing-3199996 AniDUB-3199114 AniLibria-3204688 AniMaunt-3204934 AnimeVost-3198662 AniPlay-3199009 AniRise-3199879 AniStar-3198674 Anything Group-3210309 KBK-3199048 KitsuneBox-3201994 Risens Team-3200688 SHIZA Project-3199154 TAKEOVER Project-3199180 AniDUB-3206565 AniLibria-3214108 AnimeVost-3204770 AniRise-3214571 AniStar-3204751 Anything Group-3213831 KBK-3206322 KitsuneBox-3211200 Risens Team-3205028 SHIZA Project-3210273 AniDUB-3216986 AniLibria-3218426 AnimeVost-3214254 AniRise-3219614 AniStar-3214057 Anything Group-3214746 KBK-3219664 Risens Team-3214608 SHIZA Project-3215849 AnimeVost-3218870 AniStar-3218862 Anything Group-3219909 Risens Team-3218760 

Task: convert a line from the original view to such

 AnimeVost|3198662|3204770|3214254|3218870||AniStar|3198674|3204751|3214057|3218862||Anything Group|3210309|3213831|3214746|3219909||Risens Team|3200688|3205028|3214608|3218760||AniDUB|3199114|3206565|3216986||AniLibria|3204688|3214108|3218426||.....||Amazing Dubbing|3199996 

AnimeVost, AniStar, etc. - these are groups of people, 3198662, 3204770, etc. - these are works of art created by these groups of people. I need to sort the line so as to assign the group all the id that belongs to it. And the group with more id goes first, then the one with less than the first, and so on. (descending count id)

  • one
    and what are the keys here, how to break a line, what is the number of numbers? - splash58
  • All the same, confused. The text part (AniLibria, AniDUB ...) is a group. To their left is the id of the piece, separated from the group by the symbol | The groups themselves are separated by the symbol || The string consists of four parts, which are separated by the symbol ||| - DomiTori Kun
  • did not understand anything about sorting. you have 4 groups in the given line with 4 IDs, the result is clearly not sorted by number, and not alphabetically - teran
  • Sorted by sets, separated by ||| Such sets of 4 if you look closely. Inside of them there are groups of people and id of their works. I need to bring the line to the form: group | id | id | id | id || next group | id ... - DomiTori Kun
  • describe the normal task. what and how to break it is necessary, etc. with an example of how to divide the string, what we get, what we do next. Edit your question using edit - teran

2 answers 2

You can divide it like this, but then what should you do?

 $all = explode('|||', $str); foreach($all as $i) { $temp = explode('||', $i); $arr = []; foreach($temp as $x) { list($v, $k) = explode('|', $x); $arr[] = [$k =>$v]; } $arr = array_merge_recursive(...$arr); $res[] = $arr; } print_r($res); 

https://eval.in/946366

  • Further down the line I need to get this AnimeVost-3198662,3204770,3214254,3218870. AniStar-3198674,3204751,3214057,3218862. Anything Group-3210309,3213831,3214746,3219909. Risens Team-3200688,3205028,3214608,3218760. AniDUB-3199114,3206565,3216986. AniLibria-3204688,3214108,3218426. Amazing Dubbing-3199996 AnimeVost-3198662,3204770,3214254,3218870. AniStar-3198674,3204751,3214057,3218862. Anything Group-3210309,3213831,3214746,3219909. Risens Team-3200688,3205028,3214608,3218760. AniDUB-3199114,3206565,3216986. AniLibria-3204688,3214108,3218426. Amazing Dubbing-3199996 AnimeVost-3198662,3204770,3214254,3218870. AniStar-3198674,3204751,3214057,3218862. Anything Group-3210309,3213831,3214746,3219909. Risens Team-3200688,3205028,3214608,3218760. AniDUB-3199114,3206565,3216986. AniLibria-3204688,3214108,3218426. Amazing Dubbing-3199996 - DomiTori Kun
  • add to the end of my code $res = array_merge_recursive(...$res); and it remains to sort it with usort (). There are many such examples - splash58
  • Thank. Somehow I missed this function in Google to merge arrays. What you need - DomiTori Kun
  $str="3199996|Amazing Dubbing||3199114|AniDUB||3204688|AniLibria||3204934|AniMaunt||3198662|AnimeVost||3199009|AniPlay||3199879|AniRise||3198674|AniStar||3210309|Anything Group||3199048|KBK||3201994|KitsuneBox||3200688|Risens Team||3199154|SHIZA Project||3199180|TAKEOVER Project|||3206565|AniDUB||3214108|AniLibria||3204770|AnimeVost||3214571|AniRise||3204751|AniStar||3213831|Anything Group||3206322|KBK||3211200|KitsuneBox||3205028|Risens Team||3210273|SHIZA Project|||3216986|AniDUB||3218426|AniLibria||3214254|AnimeVost||3219614|AniRise||3214057|AniStar||3214746|Anything Group||3219664|KBK||3214608|Risens Team||3215849|SHIZA Project|||3218870|AnimeVost||3218862|AniStar||3219909|Anything Group||3218760|Risens Team"; $str=str_replace('|||','||',$str); $mass=explode("||", $str); $str2=""; foreach ($mass as $key => $value) { $mass2=explode("|", $value); $str2=$str2.",".$mass2[0].",".$mass2[1]; } $str2=substr($str2, 1); $mass3=explode(",", $str2); $c=count($mass3); for($i=0;$i<=$c;$i++){ if($i & 1){$arrr[$mass3[$i-1]]=$mass3[$i]; $unic_text[$mass3[$i]]=$mass3[$i-1];} } foreach ($unic_text as $key => $value) { foreach ($arrr as $key1 => $val) { if($key==$val){ $masiv[$key]=$masiv[$key]."|".$key1; }} } print_r($masiv); 

Somehow, then usort, and only then implode. Although I think there is a more elegant solution.

  • would you like to get your hands on, for this code formatting - teran
  • Alas, such a heresy is stored in the database. And from porridge I would like to make shrimp) - DomiTori Kun
  • I'm sorry, I'm a fan of psr did not read (((( - Alex Valenchits
  • To indent text in the text you do not need to read psr - teran