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++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?