explode() into $key=>$value pair

$my_string = "key0:value0,key1:value1,key2:value2";

$convert_to_array = explode(',', $my_string);

for($i=0; $i < count($convert_to_array ); $i++){
    $key_value = explode(':', $convert_to_array [$i]);
    $end_array[$key_value [0]] = $key_value [1];
}

Outputs array

$end_array(
            [key0] => value0,
            [key1] => value1,
            [key2] => value2
            )

Don't believe this is possible in a single operation, but this should do the trick:

list($k, $v) = explode(' ', $strVal);
$result[ $k ] = $v;

You can loop every second string:

$how_many = count($array);
for($i = 0; $i <= $how_many; $i = $i + 2){
  $key = $array[$i];
  $value = $array[$i+1];
  // store it here
}

$strVar = "key value";
list($key, $val) = explode(' ', $strVar);

$arr= array($key => $val);

Edit: My mistake, used split instead of explode but:

split() function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged

Tags:

Php