I have a string like:
$string = "/key/value/anotherKey/anotherValue/thirdKey/thirdValue"
I would like to parse this string to an array of key => value:
array(
key => value,
anotherKey => anotherValue,
thirdKey => thirdValue
);
The only solution I have found is the following, but it seems that there should be an easier way to achieve my goal:
$split = preg_split("[/]", $string, null, PREG_SPLIT_NO_EMPTY);
$i = 0;
$j = 1;
$array = [];
for(;$i<count($split)-1;){
$array[$split[$i]] = $split[$j];
$i += 2;
$j += 2;
}
I have looked at this SO post, put it only works for query strings like:
"key=value&anotherKey=anotherValue&thirdKey=thirdValue"
Can anyone guide me towards a more "simple" solution (by that I mean less code to write, the usage of PHP functions and I would like to prevent using loops)? Thank you!