PHP--Warning: Invalid argument supplied for foreach() in ...
1.背景
今天学习PHPExcel的使用,在代码执行foreach($data as $value){...}的时候出现这样一个警告提示:Warning: Invalid argument supplied for foreach() in I:\WWW\PHPExcel\export.php on line 35;后来查询了一下,这个警告的意思是:foreach()遍历输出的是一无效的数组【注意,无效数组不等于空数组】,而导致我这个错误的原因是
【getType($i)这个方法,属于数据库操作,我把所有的数据库操作封装成了方法写在了另一个文件中,这里没有需要对象来调用,因此没有取到数据导致数组为空,从而导致$data这个是一个无效数组,从而引起了报错!】
2.摘要
为了代码在正式上线运行中频繁报错:
我们应该在遍历之前对数组变量进行一个判断:如果该变量不是一个有效数组,则设置该变量为一个空数组即array(),这样是较好的解决办法!【额(⊙o⊙)…但是像我这样,马大哈了,在测试的时候还是先把判断注释掉,这样才能知道到底是哪里错了!因为查了怎么解决这个警告提示,所以特此记录,如果有其他更好方法或想法,欢迎指正!】
3.解决方法
【在此,我使用三元运算符简便了判断数组是否为有效数组】
is_array( $data = $db->getType($i)) ? null : $data=array();
这行代码的意思:判断$data是否为一个有效数组,如果数组有效,继续下列操作,如果数组是无效数组,则给$data变量赋一个空数组值
【is_array():返回一个布尔值,功能是判断传入的变量是否是一个有效的数组!注意,空数组 $data=array() 是有一个空值的,是一个有效数组,自己可以测试一下】
PHP--Warning: Invalid argument supplied for foreach() in ...的更多相关文章
- foreach的参数不是数组:Warning: Invalid argument supplied for foreach
Warning: Invalid argument supplied for foreach() 问题Warning: Invalid argument supplied for foreach() ...
- Warning: Invalid argument supplied for foreach()
经常对提交过来的数据进行双重循环,但是为空时会报错:Warning: Invalid argument supplied for foreach() 如下解决即可:foreach($data[$i] ...
- NOTIC: Invalid argument supplied for foreach()
NOTIC: [2] Invalid argument supplied for foreach() Warning: Invalid argument supplied for foreach() ...
- php foreach 语法的遍历来源数组如果不是一个有效数组php会出现错误警告 Invalid argument supplied for foreach()
在php中,foreach语法的遍历来源数组如果不是一个有效数组,php会出现错误警告 Invalid argument supplied for foreach() ,但是很多时候这个数组是取自某些 ...
- Yii 提示Invalid argument supplied for foreach() 等错误
Yii 提示Invalid argument supplied for foreach() 或者 undefined variable: val等错误 只需要在对应的文件中加入error_report ...
- PHP:Invalid argument supplied for foreach()错误原因及解决办法
在php中使用foreach循环遍历时报Invalid argument supplied for foreach()错误,是因为循环的数据不是一个有效的数组. 因此我们只要在foreach之前判断一 ...
- Invalid argument supplied for foreach()
将需要被遍历的数组强制转换为数组类型即可 <?php $array = null; foreach((array)$array as $value){ #..code.... } ?>
- move_base Warning: Invalid argument "/map" passed to canTransform argument target_frame的解决方法
把global_costmap_params.yaml和local_costmap_params.yaml文件里的头几行去掉“/”,然后重新编译就可以了. 效果如下:
- WARNING: Re-reading the partition table failed with error 22: Invalid argument
在划分磁盘分区时,遇到错误"WARNING: Re-reading the partition table failed with error 22: Invalid argument&qu ...
随机推荐
- Thinkphp源码分析系列(二)–引导类
在上一章我们说到,ThinkPHP.php在设置完框架所需要的变量和调教好环境后,在最后调用了 Think\Think::start(); 即Think命名空间中的Think类的静态方法start ...
- 有了第一台自己开发的pro,开心,明天分享最近整理逆向分析ios的一些东西
最近都在忙于ios深入研究,研究别人的代码,别人的app.然后顺藤摸瓜的找到了关键:逆向,动态特性等. 相关工具:reveal, cycript等. 特别感谢前人的分享,为了打开学习ios的另外一扇大 ...
- Redmine2.5+CentOS6+Apache2
redmine是使用ruby开发的一款无任何商业限制且可自行部署的项目管理软件,其简洁的界面比较符合程序猿的定位,使用起来比较方便,由于我之前装3X没 成功,各版本之间的依存和配置都不一样,所以最后参 ...
- 处理返回结果(XML)
var xmlHttp function showUser(str) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Br ...
- UVa 490 - Rotating Sentences
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=s ...
- shell-自动更改LINUX服务器IP
#!/bin/bash echo echo == fi i= newgateway= newhostname= cat >>$ipfile<<EOF IPADDR=&q ...
- 解决IE中window.open打开链接refer丢失的问题
来源: http://www.coderanch.com/t/114767/HTML-CSS-JavaScript/nClick-window-open-loses-referrerHello,I h ...
- CentOS 安装 chrome 浏览器
安装 google-chrome 浏览器,由于众所周知的原因,一直安装不了,下面介绍一种新方法. cd 到 /etc/yum.repos.d 创建一个yum新源 vi chromium-el6.rep ...
- asp.net GDI+绘制矩形渐变
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 关于form验证的处理片断
public virtual void SignIn(s_User user, bool createPersistentCookie) { var now = DateTime.UtcNow.ToL ...