tapaljor tapaljor - 14 days ago 8
PHP Question

How to convert string to PHP array?

I have the following string

sender=48&destination=51&message=hi+good&sender=48&destination=49&message=good+boy


Please help me convert that into PHP array as following

array = array(
'sender'=>48,
'destination'=>51,
'message'=>hi+good,
'sender'=>48,
'destination'=>49,
'message'=>good+boy
);


Note: Its not PHP GET.

Answer

This should work as inteded, to solve this problem, you just need to use explode() correctly, otherwise it's easy.

Here you go :

$firstarr=explode('&',$yourstring);
$desiredarr=array();
foreach($firstarr as $pair)
{
    $exp_pair=explode('=',$pair);
    $desiredarr[$exp_pair[0]]=$exp_pair[1];
}

print_r($desiredarr);