Invalid argument supplied for foreach() Using JSON

Asked

Viewed 529 times

1

I’m trying to traverse the foreach with a populated JSON array. To make it easier I’m testing via $_GET (but I’ve tried it with an array populated by php but it didn’t work). The $_GET is like this:

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

The foreach is like this:

foreach ($amigos as $a)

And the data passed via $_GET is like this:

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

Always returns me the error message:

Invalid argument supplied for foreach()

I don’t understand why this is happening. Any ideas?

att

1 answer

0


It’s a hair problem, change to quotes:

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

    doing so and just removing the simple quotes before and after the array works via $_GET, I will test using php to generate the array and return here..

  • It worked @Angularman?

  • It did work @Miguel, thanks for your help..

  • You’re welcome @Angularman, if the answer helped please accept it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.