PHP中的非法字符串偏移量错误

PHP中的非法字符串偏移量错误,php,laravel,laravel-4,Php,Laravel,Laravel 4,我将这些对象发送到服务器以获取记录并保存它。但它给了我下面给定的错误 array(3) { [0]=> array(1) { ["answer"]=> string(218) "{"id":"19","question_id":"10","answer_text":"Please note that we also maintain you:", "iscorrect":"1", "created_at":"2014-06-07 07:52:12", "upd

我将这些对象发送到服务器以获取记录并保存它。但它给了我下面给定的错误

array(3) {
[0]=>
array(1) {
    ["answer"]=>
    string(218) "{"id":"19","question_id":"10","answer_text":"Please note that we also maintain you:",  
"iscorrect":"1",
"created_at":"2014-06-07 07:52:12",  
"updated_at":"2014-06-07 07:52:12"}"
  }
  [1]=>
 array(1) {
   ["answer"]=>
    string(218) "{"id":"20","question_id":"10","answer_text":"Please note that we also maintain the following pages and support forums to help you:",  
"iscorrect":"1",
"created_at":"2014-06-07 07:52:13",  
"updated_at":"2014-06-07 07:52:13"}"
 }
[2]=>
array(1) {
  ["answer"]=>
  string(218) "{"id":"21","question_id":"10",  
  "answer_text":"Please note that we also maintain the following pages and support forums to help you:","iscorrect":"0","created_at":"2014-06-07 07:52:13","updated_at":"2014-06-07 07:52:13"}"
}
}

{"error":{"type":"ErrorException","message":"Illegal string offset     'id'","file":"\/home\/learnomatics\/laravel\/mobillz_mlmalp\/app\/controllers\/QuizController.php","line":379}}
我试图得到如下值

$answers=Input::get('answers');
var_dump($answers);
foreach ($answers as $answer) {
    if(isset($answer['answer'])){
        quiz_que_ans_id=$answer['answer']['id'];
}
}

这里您的答案包含一个json字符串。在获取任何密钥之前对其进行解码。 试试这个:

foreach ($answers as $answer) {
    if(isset($answer['answer'])){
        $ans = json_decode($answer['answer'],true);
        if(is_array($ans)){
            quiz_que_ans_id=$ans['id']; 
        }
    }
}

$answer['answer']
变量是一个包含JSON编码数据的字符串;要访问该文件,您需要首先对其进行解码:

$data = json_decode($answer['answer'], true);
$quiz_queue_ans_id = $data['id']; // work with decoded data

尝试以下操作,将照片传递到
Form::file
facade

{!! Form::file('photo', $user_master->photo, ['class' => 'control-label input-sm', 'type'=>'file', 'accept'=>'image/*']) !!}
而不是

{!! Form::file('photo', null, ['class' => 'control-label input-sm', 'type'=>'file', 'accept'=>'image/*']) !!}`

您需要解码json字符串以访问该字符串中的值。您从$answer中得到了什么??请打印($answer)…我是拉雷维尔的新手,所以你能告诉我如何解码json吗?使用哪种方法?您不需要Laravel来解码JSON,只需使用
JSON\u decode()
.Array(=>{“id”:“19”,“问题id”:“10”,“答案文本”:“请注意,我们还维护以下页面和支持论坛来帮助您:”,“iscorrect”:“1”,“创建时间”:“2014-06-07 07:52:12”,“更新时间”:“2014-06-07:07:52:12”)我在打印(答案)后得到了这个;它不会给出错误,但也不会获取值。表示每次显示值=null时,它不返回数组。如果我检查(is_array),它将返回false。如果不检查此情况,它将工作