explode() into $key=>$value pair
I have this:
$strVar = "key value";
And I want to get it in this:
array('key'=>'value')
I tried it with explode(), but that gives me this:
array('0' => 'key',
'1' => 'value')
The original $strVar is already the result of an exploded string, and I'm looping over all the values of the resulting array.
Don't believe this is possible in a single operation, but this should do the trick:
list($k, $v) = explode(' ', $strVal);
$result[ $k ] = $v;
$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
)
$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