Viewed   205 times

It often happens to me 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 [...]

Assuming it's not possible to refactor the get_values() function to always return an array (backward compatibility, not available source code, whatever other reason), I'm wondering which is the cleanest and most efficient way to avoid these warnings:

  • Casting $values to array
  • Initializing $values to array
  • Wrapping the foreach with an if
  • Other (please suggest)

 Answers

4

Personally I find this to be the most clean - not sure if it's the most efficient, mind!

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

The reason for my preference is it doesn't allocate an empty array when you've got nothing to begin with anyway.

Monday, September 19, 2022
4

Just modify this line

foreach ($query->result() as $row) {

To

foreach ((array)$query->result() as $row) {

Tuesday, September 20, 2022
 
4

You're looking for the array_slice function:

array_slice() returns the sequence of elements from the array array as specified by the offset...

$lines = file(...);
$lines = array_slice($lines, -50);
Thursday, October 13, 2022
 
alan
 
1

You should open and close your <rows/> in the books loop, and add a late check for odd books:

<?php 
$count = 0;
foreach ($contents as $content) 
{
    //var_dump($content);
    $books      = $content["tags"];
    $book_image = $content['content_image'];
    $book_desc  = $content['content_social_description'];


    foreach ($books as $book) 
    {
        ++$count;
        if($count == 1)
        {  
            echo "<div class='et_pb_row'>";
        }
        $book_name      = $book['tag_name'];
        $book_name_trim = str_replace(' ', '-', $book_name);
        ?>
        <!-- Inside the Book Loop -->
        <div class='et_pb_column et_pb_column_1_2 books' style="background: url('https://s3-us-west-2.amazonaws.com/crowdhubproverbs31/<?php echo $book_image ;?>');">
            <h2><?php echo $book_name; ?></h2>
            <p><?php echo $book_desc; ?></p>
            <?php echo $count; ?>
        </div>

        <?php
        if ($count == 2)
        {
            echo "</div>";
            $count = 0;
        }


    }
}

if ($count > 0)
{
    echo "</div>";
}
?>

Doing so, your $count variable will be incremented only when foreach($books AS $book) loop is run (thus you have at least one book to print)

Thursday, October 20, 2022
 
2

Foreach takes arrays and attributes each value of the array to the varname that you define. Invalid Argument simply means that the you did not provide a valid array.

For l.12, it simply means that you did not pass an array as the first arg to your function, which is due to your error line 88.

I would assume that your problem is that you haven't yet tried uploading a file and thus $_FILES is undefined. In my experience to use the $_FILES superglobal variable you usually have to upload a file via a form of enctype multi-part/formdata. To debug and check if it's defined do a var_dump or print_r on $_FILES before your foreach loop. As an added measure of security you might want to wrap said loop and your call to your mail_attachment function in an if (isset($_FILES))

Hope this helps

Tuesday, November 15, 2022
 
sawan
 
4

Change this:

$path[] = $dir;
foreach($path as $dirname){
    $add = glob($dirname . '/*.css');
    foreach($add as $file){

to this:

$path[] = $dir;
var_dump($path);
foreach($path as $dirname){
    $add = glob($dirname . '/*.css');
    var_dump($add);
    foreach($add as $file){

We don't know which line 131 is, so I don't know which foreach fails.

(I'm guessing the 2nd, because the first is practically forced to array by $path[] = ..)

Saturday, December 10, 2022
 
afri
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :