Technically, it is possible to do this without using loops (demo on codepad.org):
$string = '1-350,9-390.99';
// first, split the string into an array of pairs
$output = explode(',', $string);
function split_pairs ($str) {
return explode('-', $str, 2);
}
$output = array_map(split_pairs, $output);
// then transpose it to get two arrays, one for keys and one for values
array_unshift($output, null);
$output = call_user_func_array(array_map, $output);
// and finally combine them into one
$output = array_combine($output[0], $output[1]);
var_export($output);
However, this is really not something you'd want to do in real code — not only is it ridiculously convoluted, but it's also almost certainly less efficient than the simple foreach
-based solution others have already given (demo on codepad.org):
$output = array();
foreach ( explode( ',', $string ) as $pair ) {
list( $key, $val ) = explode( '-', $pair, 2 );
$output[$key] = $val;
}