Warning: strlen () expects parameter 1 to be a string, array given

Possible duplicate:
mysql_fetch_array () expects parameter 1 to be a resource, boolean is set to select

I will transfer my site to a new host. The previous version of php was 5.2 and now is 5.3. After I changed the php version, it displays a warning on 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;
}
+5
source share
3 answers

You can use count () to get the size of the array:

if (!is_array($arr) || !$arr || count($arr)==0)  return 0;
+3
source

You need to use count()instead strlen()to get the number of elements in the array.

. FALSE (!$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;
}
+6

If you want to check for an empty array, you need count(), and not strlen, which is valid for strings.

+1
source

All Articles