Invalid argument supplied for foreach () Using JSON

1

I'm trying to loop through foreach with an array populated in JSON. To facilitate I'm testing via $ _GET (but I already tested with array populated by php itself but did not work). $ _GET looks like this:

$amigos = json_decode($_GET['amigos']);

The foreach looks like this:

foreach ($amigos as $a)

And the data passed via $ _GET looks like this:

[{'nome':'bruno','email':'[email protected]'}]

I always get the error message:

Invalid argument supplied for foreach()

I do not understand why this happens .. Any ideas?

att

    
asked by anonymous 06.06.2016 / 23:58

1 answer

0

It's a pelican problem, switch to quotes:

<?php
$amigos = json_decode('[{"nome":"bruno","email":"[email protected]"}]');
foreach($amigos as $a) {
    echo $a->nome; // bruno
}
    
07.06.2016 / 14:22