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!
CodePudding user response:
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;
}