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

无法解决这个问题:为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条)

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写