如何在PHP中将数组转换为字符串

时间:2017-06-21 20:32:25

标签: php

我正在尝试在PHP中使用字符串转换数组,以便我可以将其存储到mySQL字段中,反过来我可以获取: 以下是数据格式:

array:2 [
  0 => array:2 [
    "year" => "234"
    "amount" => "2387"
  ]
  1 => array:2 [
    "year" => "23"
    "amount" => "324"
  ]
]

我正在尝试这样的事情:

$var = implode(',', $data['revenues']);

我尝试使用内爆,但它不起作用。请帮帮我。

修改

我之前尝试过这个:

public function store(Request $request)
{
    $this->validate($request, Research::$create_validation_rules);
    $data = $request->only(['coverage_date', 'company_id', 'contact_id', 'type', 'reco', 'target', 'projections', 'revenues', 'ebitas', 'profits']);
    $data['coverage_date'] = Carbon::parse($request->coverage_date)->toDateTimeString();
    $data['revenues'] = json_encode($data['revenues']);
    $data['projections'] = json_encode($data['projections']);
    $data['profits'] = json_encode($data['profits']);
    $research = Research::create($data);
    if($research)
    {
        return response()->json(['msg' => 'Successfully Created'], 200);
    }
    else
    {
        return response()->json(['msg' => 'Something went wrong'], 450);
    }
}

我得到的错误是:

Error screenshot

1 个答案:

答案 0 :(得分:0)

使用json_encode

例如

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
    <rules>
        <rule name="Contact" stopProcessing="true">
            <match url="^contact$" />
            <action type="Rewrite" url="/16_Contact.cfm" />
        </rule>
    </rules>
    </rewrite>
</system.webServer>
</configuration>

PHP sandbox

上查看