tags:

views:

83

answers:

2

Hi, im currently getting these error on my site:

Warning: array_keys() [function.array-keys]: The first argument should be an array on line 43

Warning: Invalid argument supplied for foreach() on line 44

Warning: array_diff() [function.array-diff]: Argument #1 is not an array on line 47

Warning: array_diff() [function.array-diff]: Argument #1 is not an array on line 48

And the source are:

42.        $tmp = $this->network->get_user_follows($this->user->id);
43.     $tmp    = array_keys($tmp->followers);
44.     foreach($tmp as &$v) { $v = intval($v); }
45.     $tmp2   = array_keys($this->network->get_group_members($g->id));
46.     foreach($tmp2 as &$v) { $v = intval($v); }
47.     $tmp    = array_diff($tmp, $tmp2);
48.     $tmp    = array_diff($tmp, array(intval($this->user->id)));

Here is the var_dump output:

bool(false) array(1) { [2]=> int(3) } 

I want to know what is the problem and how i fix it. Thanks!

+1  A: 

Your functions aren't returning arrays. On the assumption they return arrays when there's data to be had, and some sort of empty (null or false) value otherwise, a simple cast may work:

$tmp    = $this->network->get_user_follows($this->user->id);
$tmp    = isset($tmp->followers) && is_array($tmp->followers) ? array_keys($tmp->followers) : array();
foreach($tmp as &$v) { $v = intval($v); }
$tmp2   = array_keys((array)$this->network->get_group_members($g->id));
foreach($tmp2 as &$v) { $v = intval($v); }
$tmp    = array_diff($tmp, $tmp2);
$tmp    = array_diff($tmp, array(intval($this->user->id)));

But even better, do this as a diagnostic:

var_dump($this->network->get_user_follows($this->user->id), $this->network->get_group_members($g->id));

This should make it a lot more clear what's actually going on

Frank Farmer
im getting bool(false) array(1) { [2]=> int(3) } as resultany suggestion?
SKY
This is wrong. You cannot call `$tmp->followers` if `$tmp` is an array. `$tmp` must be an object. There's some horrible variable reassignment with conflicting types going on in the original code.
d11wtq
Good call, d11wtq. Updated with more appropriate code. That was sloppy of me.
Frank Farmer
A: 

get_user_follows is not supposed to return an array, it's supposed to return an object which has a property called followers. It's the followers property that's not an array, and that's actually because $tmp is FALSE when it should be said object.

The code in get_user_follows has some guard clause that is making it return FALSE under some condition. Figure out why and correct that ;)

d11wtq