I am using array functions to convert my pipe delimited string to an associative array.
$piper = "|k=f|p=t|e=r|t=m|";
$piper = explode("|",$piper);
$piper = array_filter($piper);
function splitter(&$value,$key) {
$splitted = explode("=",$value);
$key = $splitted[0];
$value = $splitted[1];
}
array_walk($piper, 'splitter');
var_dump($piper);
this gives me
array (size=4)
1 => string 'f' (length=1)
2 => string 't' (length=1)
3 => string 'r' (length=1)
4 => string 'm' (length=1)
where i want:
array (size=4)
"k" => string 'f' (length=1)
"p" => string 't' (length=1)
"e" => string 'r' (length=1)
"t" => string 'm' (length=1)
but the keys are unaltered. Is there any array function with which i can loop over an array and change keys and values as well?
It’s said in the documentation of array_walk (describing the callback function):
That means you cannot use
array_walkto alter the keys of iterated array. You can, however, create a new array with it:Still, I think if it were me, I’d use regex here (instead of “exploding the exploded”):