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

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);
    });
    
    点赞 打赏 评论

相关推荐 更多相似问题