如何将字符串转换为数组,在“ =”之前作为键,在“ =”之后作为php数组中的值

时间:2019-09-26 07:40:12

标签: php

我有'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'这种类型的字符串我想转换为数组。

在php数组中以'='为键,以'='为后

2 个答案:

答案 0 :(得分:3)

您可以在foreach循环中使用爆炸功能。 我在下面分享了一个示例,请查看:

$string = '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';

$new_array= explode(',',$string );
       foreach ($new_array as $data){
           $explode_value= explode('=',$data);
           $final_value[$explode_value[0]] = $explode_value[1];
       }
        echo "<pre>";print_r($final_value);die('mms');
output will be :
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
)

答案 1 :(得分:1)

$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);

输出:

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
)