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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵