简体   繁体   中英

Invalid argument supplied for foreach() with valid JSON

I'm confused why I'm getting the error 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:

$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()

I ran the JSON through a validator and it says it's valid. 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).

What am I doing wrong?

Thank you!

If you echo $selectedFiles after doing $selectedFiles = json_decode($runStatus['fldRawFiles'], true);

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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