2014-02-26 82 views
0

我正在和提供的foreach错误无效参数()中的代码波纹管。任何人都可以告诉我我在这里做错了什么?我正在尝试访问这里的子数组值[0]和[1]。只是要提到,我不知道现在许多值将在子数组中。解决不了这个()的foreach为无效的论点提供

我已经把上面的代码出错行的

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 
+0

迭代条件与'如果(is_array($ sessData)){的foreach ...' – pce

+0

你的代码的伎俩。让它成为答案,所以我可以接受它。谢谢。 – user2417624

+0

代码包含一些非意义上,我强烈建议您在foreach文档上阅读例如:http://php.net/manual/fr/control-structures.foreach.php – Asenar

回答

1

那是因为你不检查$sessData是否实际上是一个数组。事实上,这只是一个数组,只有一个案例正在查看你的转储。只需添加另一个if (is_array($sessData))

+0

正如我上面的评论,是的,代码和平奏效了。用户PCE上面贴的是第一,所以学分转到了他。感谢您的解释。如果他没有让他的评论的答案,我会接受你的答案,所以其他用户会更容易找到解决方案。 – user2417624

0

评论(错误所在)你有这个变量$sessData可能出现空从大循环

,并遍历它,在PHP < = 5.3它会中断:

+0

我评论说行,错误的是还有,生活,以及:-( – user2417624

+0

内部循环中添加一个条件:如果'空($ sessData){...' –

+0

对不起,我的意思外循环:d –

0

你有一个“简单的数组”,并尝试处理它作为一个多维数组。

也许你想用foreach($mySessData as $key => $val)填写变量$myresult,并使用if (is_array($val))来处理categoriesFilter键。

我不知道你到底是需要的,但是这可以帮助你:

$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 
1

你需要检查你的价值循环,虽然它之前的实际阵列。这是唯一的价值目前数组为[categoriesfilters]

相关问题