skip to Main Content

Since my host has updated php from version 7.0.3 to version 7.3.13 of php, I am getting the following error:

Invalid argument supplied for foreach()

It worked perfectly before the version change, if I use var_export on the function, with version 7.0.3 I get:

array (
    0 => 'block_menu.html',
    1 => 'block_fiches_pratiques.html',
    2 => 'block_fiche_aleatoire.html',
    3 => 'block_random_annonces.html',
    4 => 'block_consult.html',
    5 => 'block_random_oquerhtml',
    6 => 'block_user_information.html',
    7 => 'block_horoscope.html',
    8 => 'block_favoris.html',
    9 => 'block_links.html',)

If I use var_export on the function, with version 7.3.13, it shows me 10 times the first letter of the character string, not array (bbbbbbbbbbb)

Here is the offending code:

if($left_block_ary !='') {
    foreach ($left_block_ary as $block => $value){
    $template->assign_block_vars('left_block_files', array(
    'LEFT_BLOCKS'       => portal_block_template($value),
    'LEFT_BLOCK_ID'     => $left_block_id[$block],
    'LEFT_BLOCK_SOURCE' => htmlspecialchars_decode(smilies_pass(censor_text($left_block_source[$block]))),
    'LEFT_BLOCK_NOM'    => $left_block_nom[$block],                     
    ));
    } } else {}

Thank you for your help…

3

Answers


  1. Chosen as BEST ANSWER

    Thanks for your help.

    But precisely the problem is there, with the php version before 7.3.13, with the test of the var_export function ($ left_block_ary); , it shows me the table well. Since version 7.3.13, it displays 10 times the first letter of the character chain, not array (bbbbbbbbbbb).

    See my first post.

    I changed if ($ left_block_ary! = '') To if (! Empty ($ left_block_ary)), it makes the code cleaner, but of course it doesn't change the error.


  2. Your if($left_block_ary !='') is what is not checking or isn’t working correctly. $left_block_ary is an array and therefore it will never be equal to '' thus that check always passes even in cases where the array is empty instead you can use the

    if(!empty($left_block_ary)) {
       //perform your loop here
    }
    

    check to check whether the array is empty or not.

    Login or Signup to reply.
  3. The error message caused by $left_block_ary is not an array. If the variable $left_block_ary can not only be an array then you should put validation using is_array() to check if variable is an array before using foreach() and you may also use empty() to determine if has value like this example:

    if (is_array($left_block_ary)) {
    
        foreach ($left_block_ary as $block => $value){
            echo $value."<br>";
        }
    
        if (empty($left_block_ary)) {
           echo 'array is empty'
        }
    } else {
        echo 'not array';
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search