- 问题现象
~# 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。当前单元的值被赋给
key。当前单元的值被赋给value并且数组内部的指针向前移一步。
有两种语法:
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
不管哪种语法,其中参数必须为数组,因此,自定义函数调用foreach时,参数须进行数组的判断。