Home >Backend Development >PHP Tutorial > foreach 的怪有关问题

foreach 的怪有关问题

WBOY
WBOYOriginal
2016-06-13 13:07:19811browse

foreach 的怪问题
$result = mysql_query("SELECT * FROM TB");
while($row = mysql_fetch_array($result))
{
/*这种方法能成功显示
for ($i=1; $i {
  echo $row[$i]." | ";
}

   
  echo "
";
  */

  foreach ($row as $x)
  {
  echo $x." | ";

}

}
搞不明白为什么弟二种方法每个字段的数据显示了2次

------解决方案--------------------
mysql_fetch_array 返回的是下标数组和关联数组的混合数组
查询结果的每一列都要在下标数组和关联数组个出现一次

for 循环的是下标数组的部分
而 foreach 循环的是全部

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn