V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xuyl
V2EX  ›  PHP

PHP 数组 diff 的问题,求教。

  •  
  •   xuyl · 2018-04-02 10:06:07 +08:00 · 2268 次点击
    这是一个创建于 2187 天前的主题,其中的信息可能已经有所发展或是发生改变。

    写了个方法如下,不知道为啥, 最后返回的$apps的所有项的is_allow属性都是 1,也就是说 if 判断 in_array 全为 false

        public function apps()
        {
            $apps = App::all();
            $myApp = \Auth::user()->apps;
            $diffApp = $apps->diff($myApp);
    
            foreach ( $apps as &$app )
            {
                if ( in_array($app, (array)$diffApp) )
                {
                    $app->is_allow = 0;
                } else {
                    $app->is_allow = 1;
                }
            }
            return $apps;
        }
    

    foreach处理前的$apps

    [{
    	"id": 1,
    	"name": "应用 1"
    }, {
    	"id": 2,
    	"name": "应用 2"
    }, {
    	"id": 3,
    	"name": "应用 3"
    }]
    

    $diffApp的内容

    [{
    	"id": 1,
    	"name": "应用 1"
    }]
    

    foreach处理后的$apps

    [{
    	"id": 1,
    	"name": "应用 1",
    	"is_allow": 1
    }, {
    	"id": 2,
    	"name": "应用 2",
    	"is_allow": 1
    }, {
    	"id": 3,
    	"name": "应用 3",
    	"is_allow": 1
    }]
    
    第 1 条附言  ·  2018-04-02 14:09:50 +08:00
    已解决,多谢 @InternetExplorer 的思路,同样也感觉其他人的方案。
    14 条回复    2018-04-02 14:51:40 +08:00
    feiyuanqiu
        1
    feiyuanqiu  
       2018-04-02 10:18:14 +08:00
    把你的 diff 实现贴出来啊...
    InternetExplorer
        2
    InternetExplorer  
       2018-04-02 10:22:57 +08:00
    你的 is_array 写两个参数真的不报错吗
    imn1
        3
    imn1  
       2018-04-02 10:26:05 +08:00
    in_array 的顺序是不是搞错了?
    b821025551b
        4
    b821025551b  
       2018-04-02 10:26:57 +08:00
    in_array 里面,$app 是一个 object,(array)$diffApp 是一个二维数组包的 object,能一样可就出鬼了。
    imn1
        5
    imn1  
       2018-04-02 10:28:36 +08:00
    回上一条,我眼花了,好像是我搞错了
    InternetExplorer
        6
    InternetExplorer  
       2018-04-02 10:34:44 +08:00
    我...看花了,没睡醒
    Vogan
        7
    Vogan  
       2018-04-02 10:36:13 +08:00
    The in_array function cannot compare objects.
    xuyl
        8
    xuyl  
    OP
       2018-04-02 10:37:15 +08:00
    @feiyuanqiu diff 方法是 laravel 自带的...
    InternetExplorer
        9
    InternetExplorer  
       2018-04-02 10:37:40 +08:00
    从代码来看,应该是 laravel,$apps 应该是一个 App model 的数组,那么 foreach 循环里的 $app 应该是个对象,in_array 在比较的时候可以认为是 == 比较,所以两个 object 是不会相等的,都是返回的 false
    可以考虑取出 $apps 的所有 id 来比较
    InternetExplorer
        10
    InternetExplorer  
       2018-04-02 12:55:20 +08:00
    我可能又说错了......
    (array)$diffApp) 得到的东西你 dd 出来看一下就知道为什么了。
    我补觉去了(~﹃~)~zZ
    HYSS
        11
    HYSS  
       2018-04-02 12:58:09 +08:00
    又黑我 PHP
    wanghanlin
        12
    wanghanlin  
       2018-04-02 13:22:41 +08:00
    ```
    return App::select('apps.*', DB::raw('IF(app_user.id IS NULL, false, true) as is_allow'))
    ->leftJoin('app_user', function($query){
    return $query->on('app_user.app_id', 'apps.id')
    ->where('user_id', auth()->id())
    ->orWhereNull('user_id');
    })
    ->get()
    ```
    wanghanlin
        13
    wanghanlin  
       2018-04-02 13:28:22 +08:00
    格式没了,不过 SQL 能做的还是不用放 php 里面吧。
    vincenttone
        14
    vincenttone  
       2018-04-02 14:51:40 +08:00
    看起来是 in_array(Array1, Array2)了,估计楼主的 Array2 是个两层的数组,数组没法这样比较。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3297 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 13:50 · PVG 21:50 · LAX 06:50 · JFK 09:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.