Wi, I have string:
sortAfter=1&searchString=vsfweew
How can I convert it to array:
Array
(
[sortAfter] => 1
[searchString] => vsfweew
)
?
Wi, I have string:
sortAfter=1&searchString=vsfweew
How can I convert it to array:
Array
(
[sortAfter] => 1
[searchString] => vsfweew
)
?
$str = 'sortAfter=1&searchString=vsfweew';
parse_str($str, $arr);
print_r($arr);
While parse_str() is certainly convenient, there is another option using preg_split() as follows:
<?php
$str = "sortAfter=1&searchString=vsfweew";
list($e1,$sortAfter,$e2,$searchString) = preg_split("/=|&/",$str);
$arr[$e1] = $sortAfter;
$arr[$e2] = $searchString;
print_r($arr);
See live code
Preg_split() splits a string into an array whose values can then be assigned to a list of variables which include in this case variables that correspond to element names while others refer to element values. The list variables are then used to build a new array $arr.