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

时间:2014-06-09 11:54:30

标签: 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",  
"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'];
}
}

3 个答案:

答案 0 :(得分:6)

$answer['answer']变量是一个包含JSON编码数据的字符串;要访问它,您需要先解码它:

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

答案 1 :(得分:1)

这里你的答案包含一个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']; 
        }
    }
}

答案 2 :(得分:0)

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

{!! 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/*']) !!}`