Skip to content
Advertisement

how to convert string to array Before ‘=’ as key and after ‘=’ as value in php array

i have ‘refrence_no=612,app_type=1,applicant_name=erf,mobile=9685362536,file_body=sdfsdf,app_from=5,created_at=2019-09-16,created_atTo=2019-09-10’ this type of string i want to convert in array.

Before ‘=’ as key and after ‘=’ as value in php array

Advertisement

Answer

$str = "refrence_no=612,app_type=1,applicant_name=erf,mobile=9685362536,file_body=sdfsdf,app_from=5,created_at=2019-09-16,created_atTo=2019-09-10";
$array = explode(",",$str);
$newarray = [];
foreach($array as $item){
    $temp = explode("=",$item);
    $newarray[$temp[0]] = $temp[1];
}
print_r($newarray);

output:

Array
(
    [refrence_no] => 612
    [app_type] => 1
    [applicant_name] => erf
    [mobile] => 9685362536
    [file_body] => sdfsdf
    [app_from] => 5
    [created_at] => 2019-09-16
    [created_atTo] => 2019-09-10
)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement