这是我的数组
$datas = array(array('studentid' => '9','toschool' => '4','tohome'=>'4'),array('studentid' => '10','toschool' => '4','tohome'=>'4'));
当我返回时,我正在
return $data;
输出:
[{"studentid":"9","toschool":"4","tohome":"4"},{"studentid":"10","toschool":"4","tohome":"4"}]
我想从邮递员那里得到同样的数组
所以,我将名称的输出粘贴为数据
我收到它
$gotdata = Input::get('data');
当我打印时我得到了相同的输出
[{"studentid":"9","toschool":"4","tohome":"4"},{"studentid":"10","toschool":"4","tohome":"4"}]
当我试图保存记录时,$ data有效
MYModel::insert($data);
但是
MTIServiceAttendance::insert($gotdata);
它将错误抛出
Argument 1 passed to Illuminate\Database\Query\Builder::insert() must be of the type array, string given
我如何解决这个问题,以便保存$ gotdata。
注意:对于Model,输入应为
MTIServiceAttendance::insert(array(array('studentid' => '9','toschool' => '4','tohome'=>'4'),array('studentid' => '10','toschool' => '4','tohome'=>'4')));
如何使输入表单像这个数组一样?
返回:
return var_dump($data);
输出:
array(2) { [0]=> array(3) { ["studentid"]=> string(1) "9" ["toschool"]=> string(1) "4" ["tohome"]=> string(1) "4" } [1]=> array(3) { ["studentid"]=> string(2) "10" ["toschool"]=> string(1) "4" ["tohome"]=> string(1) "4" } }
返回:
return var_dump($gotdata);
输出:
string(94) "[{"studentid":"9","toschool":"4","tohome":"4"},{"studentid":"10","toschool":"4","tohome":"4"}]"
答案 0 :(得分:1)
$gotdata
是数组的JSON字符串表示形式。因此,虽然在第一个输出中看起来相同,但在使用var_dump
时可以清楚地看到差异。只需使用json_decode
将其转换为数组:
$gotdata = Input::get('data');
$gotdata = json_decode($gotdata, true);
MTIServiceAttendance::insert($gotdata);