2

I have this warning at line 5 , Warning: Invalid argument supplied for foreach()

if (!function_exists('get_user_online_count')) {
    function get_user_online_count($type=false,$full=true) {
        global $counter_matrix;
        $user_types=array();
        foreach($counter_matrix as $key=>$val){
            if(!isset($user_types[$val['user_type']])){
                $user_types[$val['user_type']] = 0;
            }
            $user_types[$val['user_type']] ++;
        }
        if($full){
            $print = '';
            while(count($user_types)){
                $user_type = key($user_types);
                $user_count = array_shift($user_types);
                if($type && $user_type != $type)continue;
                if($print!=''){
                    if(count($user_types))$print .= ', ';
                    else $print .= ' and ';
                }else{

                }
                $print .= $user_count . ' ' . $user_type . (($user_count>1)?'s':'');
            }
            if(!$print){
                if($type){
                    $print = '0 '.$type.'s online';
                }else{
                    $print = '0 Users online';
                }
            }else{
                $print .= ' online';
            }
            return $print;
        }else{
            if($type){
                return (isset($user_types[$type])) ? $user_types[$type] : 0;
            }else{
                return count($counter_matrix);
            }
        }
    }
}

i have warning at this line 'foreach($counter_matrix as $key=>$val){'

Whats the wrong here ???

Milan Milosevic
  • 413
  • 2
  • 8
  • 19

1 Answers1

2

The reason is your global variable $counter_matrix is not an array.

You can check placing this right before your foreach:

echo gettype($counter_matrix);

That will tell you the type of your variable, alternatively you can also use var_dump($counter_matrix) which will also print the contents.

Nelson
  • 49,283
  • 8
  • 68
  • 81
  • echo gettype($counter_matrix); result is NULL – Milan Milosevic Nov 25 '12 at 12:53
  • That means you don't have a global `$counter_matrix` variable, check out for typos in the variable name, review where that variable is created, make sure it's at the global scope (not inside any function). – Nelson Nov 25 '12 at 12:57