function sortpm($parameters){
if(strpos($parameters,"&")!==false){
foreach(explode("&",$parameters) as $pmsplit){
$key = explode("=",$pmsplit)[0];
$value = explode("=",$pmsplit)[1];
$fields[]=[$key=>$value];
}
$parameters = ksort($fields);
}else{
$parameters = $parameters;
}
print_r($parameters);
}
when i get sortpm("z=4&a=2"); array are not sorted by keys
i want this output: Array ( [a] => 2[z] => 4 )
ksortwill sort the$fieldsarray and will return a boolean. You would want something like this:There are also some unnecessary things happening here. I would rewrite your code like this:
Alternatively you could use parse_str as AymDev pointed out.