问题
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
I' moving my website to a new host. The previous php version was 5.2 and now is 5.3. After I changed the php version, it shows the warning almost every page:
strlen() expects parameter 1 to be string, array given
The error line is the third line in this function:
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;
}
回答1:
You should use count() to get the size of an array:
if (!is_array($arr) || !$arr || count($arr)==0) return 0;
回答2:
You need to use count()
instead of strlen()
to get the number of elements in an array.
However, you don't need anything at all. An empty array will evaluate to FALSE
earlier than this (!$arr
) so this check is unnecessary.
This is how I would write your function (edited):
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;
}
回答3:
If you want to check for an empty array, you need count()
, not strlen
which is indeed for strings.
来源:https://stackoverflow.com/questions/7259179/warning-strlen-expects-parameter-1-to-be-string-array-given