Jun*_*uni 6 php warnings strlen
我将我的网站转移到新主机上.以前的php版本是5.2,现在是5.3.我更改了php版本后,几乎每个页面都显示警告:
strlen() expects parameter 1 to be string, array given
Run Code Online (Sandbox Code Playgroud)
错误行是此函数中的第三行:
function implodestr($arr,$field) {
unset($out_str);
if (!is_array($arr) || !$arr || strlen($arr)==0) return 0; //error line
foreach($arr as $k=>$v) {
$out_str.= $v[$field].",";
}
$str = trim($out_str,",");
$str ? "": $str=0;
return $str;
}
Run Code Online (Sandbox Code Playgroud)
您需要使用count()而不是strlen()获取数组中的元素数.
但是,您根本不需要任何东西.空数组的计算结果将FALSE早于this(!$arr),因此不需要进行此检查.
这就是我编写你的函数(编辑)的方法:
function implodestr ($arr, $field) {
// Make sure array is valid and contains some data
if (!$arr || !is_array($arr)) return FALSE;
// Put the data we want into a temporary new array
$out = array();
foreach ($arr as $v) if (isset($v[$field])) $out[] = $v[$field];
// Return the data CSV, or FALSE if there was no valid data
return ($out) ? implode(',',$out) : FALSE;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23520 次 |
| 最近记录: |