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

时间:2018-12-22 23:28:56

标签: php arrays laravel duplicates

我想从$allPermissions数组中的$userPermissions数组中删除值。

该怎么做?我已经合并了array_uniquearray_diff,但我仍然没有解决这个问题。有人吗

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');
    }
}

编辑:

这些是var_dump$userPermissions$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 个答案:

答案 0 :(得分:0)

假设您的数组有效,则可以使用array-filter

$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);
});  

这将导致$filteredPermission仅包含用户权限数组中不存在的元素

已编辑:如果要保留对象数组,可以使用:

$userPermissionIds = array();
foreach($userPermissions as $per)
    $userPermissionIds[] = $per->id;

$filteredPermission = array_filter($allPermissions , function($elem) use ($userPermissionIds) {
    return !in_array($elem->id, $userPermissionIds);
});