3v4l.org

run code in 300+ PHP versions simultaneously
<?php $strings = ["cat:others;id:4,9,13", "id:4,9,13;cat:electric-products", "id:4,9,13;cat:foods", "cat:drinks,foods"]; $cats = []; $ids = []; foreach ($strings as $key=>$string) { $temps = explode(';', $string); foreach ($temps as $temp) { $tempnest = explode(':', $temp); if ($tempnest[0] === "cat") { $cats = array_merge(explode(',', $tempnest[1]), $cats); } if ($tempnest[0] === "id") { $ids = array_merge(explode(',', $tempnest[1]), $ids); } } } print_r(array_unique($cats)); print_r(array_unique($ids));

preferences:
26.55 ms | 407 KiB | 5 Q