JSON中的数组到字符串的转换

时间:2019-02-01 10:30:52

标签: arrays json laravel implode

这是我需要内爆的示例JSON数据。在这段时间里,我遇到了数组到字符串转换的错误。

JSON

{
"user_id" :"110" ,
"parent_id": "115",
"relation_name" : "justin",
"data" :[
{"relation_ship": "brohter"},   
{"relation_ship": "sister"}
],
"sum_assured": "89745$-$48721",
"annual_primium":"00000$-$0006557",
"paid_status" : "0$-$1",
"ins_company_name" : "yes",
"renew_date": "2018-10-11$-$2018-10-23"
}

这是我的内爆功能,请帮助我解决此问题。

<?php

if (isset($data)) {
    foreach ($data as $value) {
        $data_insert['user_id'] = $user_id;
        $data_insert['parent_id'] = $user_id;
        $data_insert['relation_name'] = $data['relation_name'];
        $data_insert['relation_ship'] = implode("$-$", $data['data']);
        $data_insert['sum_assured'] = implode("$-$", $data['sum_assured']);
        $data_insert['annual_primium'] = implode("$-$", $data['annual_primium']);
        $data_insert['paid_status'] = implode("$-$", $data['paid_status']);
        $data_insert['renew_date'] = implode("$-$", $data['renew_date']);
        $data_insert['ins_company_name'] = implode("$-$", $data['ins_company_name']);
        DB::table('health_tbl')->insert($data_insert);
    }

    return response()->json(['status' => 'Success', 'message' => 'Success']);
}

return response()->json(['status' => 'Failure', 'message' => 'Failed']);

1 个答案:

答案 0 :(得分:0)

您不能内爆不是数组的东西。基本上,Implode用于转弯这样的东西:

array('something', 'something else')

变成这样:

implode('<delim>', array('something', 'something else')) => "something<delim>something else"

您打算爆炸它们以将它们撕成自己的阵列吗?

在这种情况下,请像这样使用爆炸:

explode('$-$', $data['paid_status']);

这将返回(使用上面的数据):

array("0", "1")