Answer the question
In order to leave comments, you need to log in
Make an array of type (key => value) from a string?
There is a line : $ne_do_array = "key:val|key2:val2|key3:val3";
We need an array :
$array = [
'key' => 'val',
'key2' => 'val2',
'key3' => 'val3'
];
Answer the question
In order to leave comments, you need to log in
function parse($str) {
$parsed = [];
foreach (explode("|", $str) as $elem) {
list($key, $val) = explode(":", $elem);
$parsed[$key] = $val;
}
return $parsed;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question