dongxi3209 2010-04-25 05:54
浏览 31
已采纳

function.array-diff问题!

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!

  • 写回答

2条回答 默认 最新

  • dqdtgiw4736 2010-04-25 06:00
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置