Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2018
    Posts
    165
    Plugin Contributions
    4

    Default Error when going to php7, no clue why.

    I am getting the following error in my error log:

    PHP Warning: Invalid argument supplied for foreach()

    This is the function:

    PHP Code:
    function pzen_megamenu(){
                    global 
    $languages_id$db$pzen_menu;
                    
    $cat_array = array();

                    
    $menulist $pzen_menu['megamenu'];
                    
    $zen_CategoriesUL $pzen_menu['catul_ar'];
                    
    $cat_array $zen_CategoriesUL->data;

                    foreach(
    $cat_array[0] as $k0=>$v0){ //THIS LINE CAUSING THE ERROR
                            /**================================================================================================
                            **Add menuitem marked
                            **===============================================================================================*/
                            
    $subcat_marked=$bdg_type=get_pzen_options("subcat_marked_".$k0);
                            if(
    $subcat_marked==1){
                                    
    $menulist str_replace('[MEGAMENU__SUBMENU--MARKED]','megamenu__submenu--marked',$menulist);
                            }else{
                                    
    $menulist str_replace('[MG-MARKED ID="'.$k0.'"]','',$menulist);
                            } 

  2. #2
    Join Date
    Jul 2012
    Posts
    16,718
    Plugin Contributions
    17

    Default Re: Error when going to php7, no clue why.

    The "why" has to do with the variable $cat_array[0] and typically means that it is not an array. Suggestion is to do something that would output the value/structure of that variable prior to its use just to know what is going on. And not only that variable but at least a few that lead up to it. Either the array $cat_array is occasionally empty (and therefore no zeroeth element or there is no further array information once that element is accessed.

    Otherwise would need to know a lot more about the structure of all those variables to provide much more about it.
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  3. #3
    Join Date
    Jan 2018
    Posts
    165
    Plugin Contributions
    4

    Default Re: Error when going to php7, no clue why.

    I looked it up and some hints let me to this; I added the following line before the function and now warning disappears.

    PHP Code:
     if (is_array($cat_array)) {
    foreach(
    $cat_array[0] as $k0=>$v0){ 

  4. #4
    Join Date
    Jul 2012
    Posts
    16,718
    Plugin Contributions
    17

    Default Re: Error when going to php7, no clue why.

    Quote Originally Posted by tmccaff View Post
    I looked it up and some hints let me to this; I added the following line before the function and now warning disappears.

    PHP Code:
     if (is_array($cat_array)) {
    foreach(
    $cat_array[0] as $k0=>$v0){ 
    Good that it does, though the real question is if $cat_array[0] is an array. Because $cat_array could be an array but not have an element in it (empty array) or it could have elements but they be identified like $cat_array['zero'] and the loop would fail again.

    I mean sure there's a little bit of code trust/expectations that may be had, but again just because a variable is an array does not mean that it has an index accessible at 0 which also is an array.
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  5. #5
    Join Date
    Jan 2018
    Posts
    165
    Plugin Contributions
    4

    Default Re: Error when going to php7, no clue why.

    Ok, isn't there like a way to debug an array like print -r or something? I am new to php still learning. Arrays for some reason get me lost, lol.

  6. #6
    Join Date
    Jul 2012
    Posts
    16,718
    Plugin Contributions
    17

    Default Re: Error when going to php7, no clue why.

    Quote Originally Posted by tmccaff View Post
    Ok, isn't there like a way to debug an array like print -r or something? I am new to php still learning. Arrays for some reason get me lost, lol.
    Yes there is: print_r($array_name);

    And if you want to include the text of that say in a variable, an echo or say as the content to a log: print_r($arrayname, true);

    Try not to get wrapped around the axle on variables that are actually arrays. In this case it appears that the variable returned ($cat_array) could come back as an array or possibly some other type, likely false (boolean) or a pair of empty quotes. If either of those two for example they would not be an array and the foreach would fail.
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  7. #7
    Join Date
    Jan 2018
    Posts
    165
    Plugin Contributions
    4

    Default Re: Error when going to php7, no clue why.

    Ok so after using the print_r this is the output:


    Array ( [0] => Array ( [1] => Array ( [name] => DECOR [categories_image] => [count] => 0 ) ) )

    This is because I have only 1 category setup for now.

  8. #8
    Join Date
    Jan 2018
    Posts
    165
    Plugin Contributions
    4

    Default Re: Error when going to php7, no clue why.

    Ok duh so it seems when I have no categories this php warning will show. I guess I can disregard this. Thanks for help

  9. #9
    Join Date
    Jul 2012
    Posts
    16,718
    Plugin Contributions
    17

    Default Re: Error when going to php7, no clue why.

    Well there is no requirement to have a category, all products (likely just a few in this case) could be in the main category. The discussion though was more about the code than what was causing the issue or the condition of the overall store. It's a happy finding when a real reason can be applied though. :)
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  10. #10
    Join Date
    Nov 2005
    Location
    los angeles
    Posts
    2,669
    Plugin Contributions
    9

    Default Re: Error when going to php7, no clue why.

    with regards to no clue why; see:

    http://php.net/manual/en/migration70.incompatible.php
    http://php.net/manual/en/migration71.incompatible.php

    now, you can have a clue.

    in addition, i find this tool great for displaying arrays (and other objects):

    https://github.com/ospinto/dBug

    you can download the file, put it here:

    includes/extra_configures/dBug.php

    and then when you want to see an object on the screen:

    new dBug($object);

    finally, not having any categories should NOT generate a warning. the code should take that into account. as php evolves, the code must as well.

    best.
    author of square Webpay.
    mxWorks has premium plugins. donations: venmo or paypal accepted.
    premium consistent excellent support. available for hire.

 

 

Similar Threads

  1. v153 Locating issue with old ZC version when enabling PHP7
    By jerrygarciuh in forum General Questions
    Replies: 4
    Last Post: 12 Oct 2017, 10:07 PM
  2. Fatal Error when going to Main Handler
    By Muhstore in forum Built-in Shipping and Payment Modules
    Replies: 8
    Last Post: 6 Oct 2008, 05:43 PM
  3. Error message when going to payment page
    By shadoh in forum PayPal Website Payments Pro support
    Replies: 27
    Last Post: 27 Jan 2008, 06:03 AM
  4. Error message when going from Paypal to store
    By LEDmod in forum General Questions
    Replies: 2
    Last Post: 2 Jul 2007, 06:10 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR