如何将数组值转换为非对象转换为PHP

时间:2019-03-14 10:02:09

标签: codeigniter postman form-data

我已经使用邮递员表单数据提交了此数据,那么如何从from-data数组转换为非对象JSON?

我必须尝试json_encode和json_decode,但无法将json或非对象结果放入控制器。

在CodeIgniter控制器中。

Array
(
    [------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition:_form-data;_name] => "first_name"

Janak
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="last_name"

Kumar
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="mobile_no"

123456789
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="country_code"

 91
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="email_id"

test.mail@gmail.com
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="password"

123456
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="device_token"

123HFDT3434
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="device_type"

2
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="agency_name"

google
------WebKitFormBoundaryv6rwIfAWUgyyzeZF

)

1 个答案:

答案 0 :(得分:0)

这是你的追求吗?

<?php
$content = <<<OUT
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="last_name"

Kumar
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="mobile_no"

123456789
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="country_code"

 91
------WebKitFormBoundaryv6rwIfAWUgyyzeZF
Content-Disposition: form-data; name="email_id"

test.mail@gmail.com'
OUT;

$clean = str_replace("\n",'',$content);
$parts = array_filter(explode('------',$clean));

$array = array();

foreach ($parts as $row){
    $parts2 = explode('name="',$row);
    $parts3 = explode('"',$parts2[1]);
    $array[$parts3[0]] = $parts3[1];
}

echo json_encode($array);
?>

我对您的Array演示文稿有疑问,因为实际上"first_name"是值,所以我不确定您是否正确输出了数据,但请告诉我。