dongmubi4375 2018-12-22 23:28
浏览 462
已采纳

PHP - 如何从数组中删除重复值(比较2个数组)

I would like to remove values from the $allPermissions array that are in the $userPermissions array.

How to do it? I've already combined array_unique, array_diff, and I still have not come to solve this problem. Someone something?

public function notAssigned($id, Request $request)
{
    $user = User::findOrFail($id);
    $userPermissions = [{"id":5,"name":"create post","guard_name":"web","created_at":"2018-12-22 02:44:35","updated_at":"2018-12-22 02:44:35","pivot":{"model_id":3,"permission_id":5,"model_type":"App\\User"}}];
    $allPermissions = [{"id":5,"name":"create post","guard_name":"web","created_at":"2018-12-22 02:44:35","updated_at":"2018-12-22 02:44:35"},{"id":6,"name":"edit post","guard_name":"web","created_at":"2018-12-22 02:44:46","updated_at":"2018-12-22 02:44:46"},{"id":7,"name":"delete post","guard_name":"web","created_at":"2018-12-22 02:44:59","updated_at":"2018-12-22 02:44:59"}];

    $permissions = array_unique(array_merge($userPermissions, $allPermissions));
    $role = auth()->user()->hasRole([2]);
    $permission = auth()->user()->can('edit user permission');

    if ( $role == true || $permission == true ) {
        return $permissions;
    } else {
        abort(403, 'Access Denied');
    }
}

EDIT:

Those are the output of var_dump to $userPermissions and $allPermissionsallPermissions:

var_dump($userPermissions):

    array(1) { [0]=> object(stdClass)#361 (6) { ["id"]=> int(5) ["name"]=> string(11) "create post" ["guard_name"]=> string(3) "web" ["created_at"]=> string(19) "2018-12-22 02:44:35" ["updated_at"]=> string(19) "2018-12-22 02:44:35" ["pivot"]=> object(stdClass)#360 (3) { ["model_id"]=> int(3) ["permission_id"]=> int(5) ["model_type"]=> string(8) "App\User" } } }

var_dump($allPermissions):

    array(3) { [0]=> object(stdClass)#374 (5) { ["id"]=> int(5) ["name"]=> string(11) "create post" ["guard_name"]=> string(3) "web" ["created_at"]=> string(19) "2018-12-22 02:44:35" ["updated_at"]=> string(19) "2018-12-22 02:44:35" } [1]=> object(stdClass)#377 (5) { ["id"]=> int(6) ["name"]=> string(9) "edit post" ["guard_name"]=> string(3) "web" ["created_at"]=> string(19) "2018-12-22 02:44:46" ["updated_at"]=> string(19) "2018-12-22 02:44:46" } [2]=> object(stdClass)#376 (5) { ["id"]=> int(7) ["name"]=> string(11) "delete post" ["guard_name"]=> string(3) "web" ["created_at"]=> string(19) "2018-12-22 02:44:59" ["updated_at"]=> string(19) "2018-12-22 02:44:59" } }
  • 写回答

1条回答 默认 最新

  • doubo1711 2018-12-22 23:56
    关注

    Assuming your array are valid you can use array-filter for that:

    $userP = '[{"id":5,"name":"create post","guard_name":"web","created_at":"2018-12-22 02:44:35","updated_at":"2018-12-22 02:44:35"}]';
    $userPermissions = json_decode($userP, true); // will convert to array.
    
    $allP = '[{"id":5,"name":"create post","guard_name":"web","created_at":"2018-12-22 02:44:35","updated_at":"2018-12-22 02:44:35"},{"id":6,"name":"edit post","guard_name":"web","created_at":"2018-12-22 02:44:46","updated_at":"2018-12-22 02:44:46"},{"id":7,"name":"delete post","guard_name":"web","created_at":"2018-12-22 02:44:59","updated_at":"2018-12-22 02:44:59"}]';
    $allPermissions = json_decode($allP , true); // will convert to array.
    
    $userPermissionIds = array_column($userPermissions, "id")
    $filteredPermission = array_filter($allPermissions , function($elem) use ($userPermissionIds) {
        return !in_array($elem["id"], $userPermissionIds);
    });  
    

    This will result with $filteredPermission to contain only element that no exist in the user permission array

    Edited: If you want to stay with your array of object you can use:

    $userPermissionIds = array();
    foreach($userPermissions as $per)
        $userPermissionIds[] = $per->id;
    
    $filteredPermission = array_filter($allPermissions , function($elem) use ($userPermissionIds) {
        return !in_array($elem->id, $userPermissionIds);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题