user2417624
user2417624

Reputation: 693

Can't solve this: Invalid argument supplied for foreach()

I am getting and error Invalid argument supplied for foreach() in the code bellow. Can anyone tell me what I am doing wrong here? I am trying to access the sub array values [0] and [1] here. Just to mention that I have no idea now many values will be in the sub array.

I have placed a comment above of the offending line of code (where the error is)

  echo "<pre>";
  print_r($mySessData);
  echo "</pre>";


Array
(
    [addtypeid] => 
    [isnew] => 
    [orderby] => 
    [geographicareaid] => 
    [catid] => 1
    [catid2] => 
    [manufacturerid] => 
    [modelid] => 
    [yearofmanufacturing_from] => 
    [yearofmanufacturing_to] => 
    [hoursused_from] => 
    [hoursused_to] => 
    [horsepowers_from] => 
    [horsepowers_to] => 
    [price_from] => 
    [price_to] => 
    [colorid] => 
    [isdamaged] => 
    [categoriesfilters] => 
    Array
        (
            [0] => 67
            [1] => 158
        )
)

 $sessData = array();
 $myresult = array();
 $val = array();   

 if (!empty($mySessData)){
     foreach ($mySessData as $sessData) { 
         // the line bellow is the offending line, where the error is thrown
         foreach ($sessData as $val) { 
             $myresult[$val]= $val; 

             foreach($filters as $f) {
                 if ($f['filterid'] == $myresult[$val]) {
                     $strWhere2 .= $myresult[$val] .",";
                 } // end if
             } // end of third foreach

         } // end of second foreach 
     } // end of first foreach
 } // end if

Upvotes: 0

Views: 91

Answers (4)

Asenar
Asenar

Reputation: 7010

You have a "simple array" and you try to handle it as a multi dimensional array.

maybe you want to use foreach($mySessData as $key => $val) to fill the var $myresult, and use a if (is_array($val)) to deal with categoriesFilter key.

I don't know what you exactly need, but this may help you :

 $myresult = array();

 if (!empty($mySessData)){
     foreach ($mySessData as $key => $val) { 
         // the line bellow is the offending line, where the error is thrown
             $myresult[$val]= $val; 
     }

     // I don't know what filters refers to, but I suppose you have to do something like this:
     $strWhere2 = '';
     foreach($filters as $f) {
       foreach($myresult['categoriesFilter'] as $categ_filter)
         if ($f['filterid'] == $categ_filter) {
           $strWhere2 .= $categ_filter .",";
     }
 } // end if

Upvotes: 0

user933791
user933791

Reputation: 381

you need to check if your value is an actual array before looping though it. The only value that is currently an array is [categoriesfilters]

Upvotes: 1

Fabian Schneider
Fabian Schneider

Reputation: 365

That is because you aren't checking if $sessData is actually an array. In fact, it's an array in only one case looking at your dump. Just add another if (is_array($sessData))

Upvotes: 1

Abed Hawa
Abed Hawa

Reputation: 1362

You have this variable $sessData might come empty from the bigger loop

and looping over it, in PHP <= 5.3 it will break:

Upvotes: 0

Related Questions