<?php $strings = [ "cat:others;id:4,9,13", "id:4,9,13;cat:electric-products", "id:4,9,13;cat:foods", "cat:drinks,foods" ]; $count = preg_match_all( '/(?:^|;)(id|cat):|\G(?!^),?([^,;]+)/', implode(';', $strings), $matches, PREG_UNMATCHED_AS_NULL ); $cat = []; $id = []; for ($i = 0; $i < $count; ++$i) { if ($matches[1][$i] !== null) { $arrayName = $matches[1][$i]; } else { ${$arrayName}[$matches[2][$i]] = $matches[2][$i]; } } var_export(array_values($id)); echo "\n---\n"; var_export(array_values($cat));
You have javascript disabled. You will not be able to edit any code.