简体   繁体   English

为具有有效 JSON 的 foreach() 提供的无效参数

[英]Invalid argument supplied for foreach() with valid JSON

I'm confused why I'm getting the error Invalid argument supplied for foreach().我很困惑为什么我收到错误 Invalid argument supplied for foreach()。 I've seen a few other threads on here with the same error but none of the solutions have worked for me.我在这里看到其他一些线程也有同样的错误,但没有一个解决方案对我有用。

I pull my JSON out of the database (into $runStatus['fldRawFiles']) and have:我将我的 JSON 从数据库中拉出(放入 $runStatus['fldRawFiles'])并具有:

$selectedFiles= json_decode($runStatus['fldRawFiles'], true);

Which echoed out gives me:回声给了我:

{"6":{"files":[],"packages":["program_data/6/packages/1646756076.zip"],"scripts":["program_data/6/scripts/MEER_munger.py"]}}

Then I try to use:然后我尝试使用:

foreach($selectedFiles as $key=>$d){    
       $programname=$this->extractor_model->get_program_name($key);
//more code here
}

But I get the error Invalid argument supplied for foreach()但是我收到错误 Invalid argument supplied for foreach()

I ran the JSON through a validator and it says it's valid.我通过验证器运行了 JSON,它说它是有效的。 I've tried leaving out the true argument, but get the same result.我试过忽略真正的论点,但得到了相同的结果。 I've also tried putting (array) in front of json_decode, but then my key is 0 and not the key that I want from the JSON (6).我也试过将 (array) 放在 json_decode 前面,但是我的密钥是 0 而不是我想要的 JSON (6) 中的密钥。

What am I doing wrong?我究竟做错了什么?

Thank you!谢谢!

If you echo $selectedFiles after doing $selectedFiles = json_decode($runStatus['fldRawFiles'], true);如果在执行$selectedFiles = json_decode($runStatus['fldRawFiles'], true);之后回显$selectedFiles ;

And you get你得到

{"6":{"files":[],"packages":["program_data/6/packages/1646756076.zip"],"scripts":["program_data/6/scripts/MEER_munger.py"]}}

Then that field contains more JSON, so decode it again那么那个字段里面多了JSON,所以再decode一下

foreach( json_decode($selectedFiles) as $key=>$d){
    echo $key;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM