Zen Cart Logo
Forums / Code Collaboration / Error when going to php7, no clue why.

Error when going to php7, no clue why.

Views: 2,742

Results 1 to 10 of 10
09 Feb 2018, 05:30
#1
tmccaff avatar

tmccaff

Zen Follower

Join Date:
Jan 2018
Posts:
157
Plugin Contributions:
0

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:

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);
                        }
09 Feb 2018, 05:58
#2
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

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.

09 Feb 2018, 06:20
#3
tmccaff avatar

tmccaff

Zen Follower

Join Date:
Jan 2018
Posts:
157
Plugin Contributions:
0

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.

 if (is_array($cat_array)) {
foreach($cat_array[0] as $k0=>$v0){
09 Feb 2018, 10:00
#4
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

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

tmccaff:

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

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.
09 Feb 2018, 18:44
#5
tmccaff avatar

tmccaff

Zen Follower

Join Date:
Jan 2018
Posts:
157
Plugin Contributions:
0

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.

09 Feb 2018, 21:43
#6
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

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

tmccaff:

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.

09 Feb 2018, 23:18
#7
tmccaff avatar

tmccaff

Zen Follower

Join Date:
Jan 2018
Posts:
157
Plugin Contributions:
0

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.

09 Feb 2018, 23:22
#8
tmccaff avatar

tmccaff

Zen Follower

Join Date:
Jan 2018
Posts:
157
Plugin Contributions:
0

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

10 Feb 2018, 00:24
#9
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

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. :)

10 Feb 2018, 17:21
#10
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,953
Plugin Contributions:
8

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.