关于PHP Warning:Invalid argument supplied for foreach()问题分析解决

  • 问题现象
~#  ls /var/logs/ -li
	2128 -rw-r--r--		1	root		root		218031 Mar 4 16:08 access.log
	2319 -rw-r--r--      1  root     	root     	 13024 Mar 4 15:56 error.log

每隔几秒钟执行 ls /var/logs/ -li 命令,发现error.log日志文件不断增大

执行cat error.log命令

2019-09-19	09:35:36:(mod_fastcgi.c .434)	FastCGI-stderr:PHP Warning:Invalid argument supplied for foreach() in /var/www/pdu_web/config.php on line 192
2019-09-19	09:35:39:(mod_fastcgi.c .434)	FastCGI-stderr:PHP Warning:Invalid argument supplied for foreach() in /var/www/pdu_web/config.php on line 192
2019-09-19	09:35:42:(mod_fastcgi.c .434)	FastCGI-stderr:PHP Warning:Invalid argument supplied for foreach() in /var/www/pdu_web/config.php on line 192
2019-09-19	09:35:45:(mod_fastcgi.c .434)	FastCGI-stderr:PHP Warning:Invalid argument supplied for foreach() in /var/www/pdu_web/config.php on line 192
2019-09-19	09:35:48:(mod_fastcgi.c .434)	FastCGI-stderr:PHP Warning:Invalid argument supplied for foreach() in /var/www/pdu_web/config.php on line 192
2019-09-19	09:35:51:(mod_fastcgi.c .434)	FastCGI-stderr:PHP Warning:Invalid argument supplied for foreach() in /var/www/pdu_web/config.php on line 192
2019-09-19	09:35:54:(mod_fastcgi.c .434)	FastCGI-stderr:PHP Warning:Invalid argument supplied for foreach() in /var/www/pdu_web/config.php on line 192
2019-09-19	09:35:57:(mod_fastcgi.c .434)	FastCGI-stderr:PHP Warning:Invalid argument supplied for foreach() in /var/www/pdu_web/config.php on line 192
2019-09-19	09:36:00:(mod_fastcgi.c .434)	FastCGI-stderr:PHP Warning:Invalid argument supplied for foreach() in /var/www/pdu_web/config.php on line 192
2019-09-19	09:36:03:(mod_fastcgi.c .434)	FastCGI-stderr:PHP Warning:Invalid argument supplied for foreach() in /var/www/pdu_web/config.php on line 192
2019-09-19	09:36:06:(mod_fastcgi.c .434)	FastCGI-stderr:PHP Warning:Invalid argument supplied for foreach() in /var/www/pdu_web/config.php on line 192
2019-09-19	09:36:09:(mod_fastcgi.c .434)	FastCGI-stderr:PHP Warning:Invalid argument supplied for foreach() in /var/www/pdu_web/config.php on line 192
2019-09-19	09:36:12:(mod_fastcgi.c .434)	FastCGI-stderr:PHP Warning:Invalid argument supplied for foreach() in /var/www/pdu_web/config.php on line 192

根据日志,告警信息每隔3秒增加,都是config.php第192行位置;
原代码如下:

function tojson($data)
{
	$strng = '[{';
	foreach($data as $key=>$val)
	{
		if($strng !='[{')
		{
			$strng .= ",";
		}
		$strng .= "\"".$key."\"";
		$strng .= ":";

		if(is_array($val))
		{	
			$strng .= '{';
			$str = ' ';
			foreach($val as $k=>$v)
			{
				if($str !='[{')
				{
					$str .= ",";
				}
				$str .= "\"".$k."\"";
				$str .= ":";
				$str .= "\"".addslashes($v)."\"";
			}
			$strng .= $str;
			$strng .= '}';
		}
		else
		{
			//$val = iconv($val,'GBK','utf-8');
			$strng .= "\"".addslashes($val)."\"";
		}
		//$strng .= "\"".addslashes($val)."\"";
	}
	$strng .= '}]';
	
	return $strng;
}

代码修改方法1:

function tojson($data)
{
	$strng = '[{';
	foreach((array)$data as $key=>$val)
	{
		if($strng !='[{')
		{
			$strng .= ",";
		}
		$strng .= "\"".$key."\"";
		$strng .= ":";

		if(is_array($val))
		{	
			$strng .= '{';
			$str = ' ';
			foreach($val as $k=>$v)
			{
				if($str !='[{')
				{
					$str .= ",";
				}
				$str .= "\"".$k."\"";
				$str .= ":";
				$str .= "\"".addslashes($v)."\"";
			}
			$strng .= $str;
			$strng .= '}';
		}
		else
		{
			//$val = iconv($val,'GBK','utf-8');
			$strng .= "\"".addslashes($val)."\"";
		}
		//$strng .= "\"".addslashes($val)."\"";
	}
	$strng .= '}]';
	
	return $strng;
}

代码修改方法2:

function tojson($data)
{
	if(is_array($data))
	{
		$strng = '[{';
		foreach($data as $key=>$val)
		{
			if($strng !='[{')
			{
				$strng .= ",";
			}
			$strng .= "\"".$key."\"";
			$strng .= ":";
	
			if(is_array($val))
			{	
				$strng .= '{';
				$str = ' ';
				foreach($val as $k=>$v)
				{
					if($str !='[{')
					{
						$str .= ",";
					}
					$str .= "\"".$k."\"";
					$str .= ":";
					$str .= "\"".addslashes($v)."\"";
				}
				$strng .= $str;
				$strng .= '}';
			}
			else
			{
				//$val = iconv($val,'GBK','utf-8');
				$strng .= "\"".addslashes($val)."\"";
			}
	}
	else
	{
		$strng .= "\"".addslashes($data)."\"";
	}	
	$strng .= '}]';
	
	return $strng;
}

foreach仅能用于数组,每次循环,当前单元的键名在每次循环中被赋给变量 k e y 。 当 前 单 元 的 值 被 赋 给 key。当前单元的值被赋给 keyvalue并且数组内部的指针向前移一步。
有两种语法:

foreach (array_expression as $value)
    statement
foreach (array_expression as $key => $value)
    statement

不管哪种语法,其中参数必须为数组,因此,自定义函数调用foreach时,参数须进行数组的判断。

最后推荐一个公众号,一枚IT技术人成长路上关于生活和职场的思考,欢迎书友们前来交流和分享心得

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值