Invalid argument supplied for foreach

0 votes

To handle data that can be either an array or a null variable and to feed some foreach with these data.

$values = get_values();

foreach ($values as $value){
  ...
}

When you feed a foreach with data that are not an array, you get a warning:

Warning: Invalid argument supplied for foreach() in [...]

 Can someone please help me with this error?

Apr 28 in Other DevOps Questions by Kichu
• 19,040 points
80 views

1 answer to this question.

0 votes

Use this:

if (is_array($values) || is_object($values))
{
    foreach ($values as $value)
    {
        ...
    }
}

This doesn't allocate an empty array when you've got nothing, to begin with anyway.

I hope this helps.

answered Apr 29 by narikkadan
• 37,660 points

Related Questions In Other DevOps Questions

0 votes
2 answers
0 votes
1 answer

how to check runtime for chef-client executions?

 Chef doesn't have the mechanism to save ...READ MORE

answered Jul 6, 2018 in Other DevOps Questions by DareDev
• 6,890 points
275 views
0 votes
1 answer
0 votes
0 answers

How to access mysql result set data with a foreach loop

I'm working on a PHP application that ...READ MORE

Jul 29 in PHP by Kithuzzz
• 20,660 points
1,574 views
0 votes
0 answers

PHP foreach loop key value

Running this DB call will provide me ...READ MORE

Aug 1 in PHP by Kithuzzz
• 20,660 points
428 views
0 votes
0 answers

How does PHP 'foreach' actually work?

Let me start by stating that I ...READ MORE

Aug 7 in PHP by Kithuzzz
• 20,660 points
57 views
+1 vote
2 answers

Scp Php files into server using gradle

Tru something like this: plugins { id ...READ MORE

answered Oct 11, 2018 in DevOps & Agile by lina
• 8,220 points
613 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP