douyou7878 2014-02-26 14:05
浏览 356
已采纳

无法解决这个问题:为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
  • 写回答

4条回答 默认 最新

  • dongsan6889 2014-02-26 14:08
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?