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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24119753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:09:38  来源:igfitidea点击:

Illegal string offset error in PHP

phplaravellaravel-4

提问by Sajid Ahmad

I am sending these objects to server to fetch the record and save it. but it is giving me the below given error

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

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}}

I am trying to get the values like

我正在尝试获得类似的值

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

回答by Ja?ck

The $answer['answer']variable is a string comprising JSON encoded data; to access that, you need to decode it first:

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

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

回答by Nishu Tayal

Here your answer contains a json string. Decode it before fetching any key. Try this :

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

回答by Bijendra Sahu

Try the following, which passes the photo into the Form::filefacade.

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

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

Instead of

代替

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