2013-10-21 27 views
-2

我有一个doubt.this是我的foreach循环的foreach并且这在PHP表示警告

<table> 
<?php foreach($this->msg as $l): ?> 
<tr><td> 
<a href="index.php/downloads?id=<?php echo $l->id;?>"><?php echo $l->name;?></a> 
</td></tr> 
<?php endforeach; ?> 
    </table> 

其中$这 - >信息是从数据库结果的数组。这显示警告

Warning: Invalid argument supplied for foreach() 

我应该如何解决这个问题?

+3

那时的你调用这个代码,'$这个 - > msg'是不是** ** 数组。你应该在进入循环之前检查它的类型(尝试'is_array'),或者在声明中将它实例化为一个空数组,例如'private $ msg = array();' – Phil

+0

使用var_dump并检查$ this-msg值 –

+0

检查$ this-> msg数组'var_dump($ this-> msg)' – vbrmnd

回答

0

的答案是:

if(empty($this->msg)){ 
} 
else{ 
<table> 
<?php foreach($this->msg as $l): ?> 
<tr><td> 
<a href="index.php/downloads?id=<?php echo $l->id;?>"><?php echo $l->name;?></a> 
</td></tr> 
<?php endforeach; ?> 
</table> 
} 
0

如果你不确定你通过的是什么foreach那么我建议你先用is_array函数检查。事情是这样的:

if (is_array($message)) { 

foreach ($message as $text) { 
//do something 

} 
} 

使用var_exportvar_dump检查什么你传递给foreach循环。

0

试试这个:

First check output from $this->msg. 
If it is array type && not empty then and then you can pass to the foreach loop. 

You can check array using is_array() function 
&& 
You can check array is empty using empty() function. 

- 感谢