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);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?