重新索引对象,以便键是顺序的

时间:2015-12-14 12:42:54

标签: php laravel object indexing collections

我正在使用laravel集合构建一个对象,其代码如下所示,

$project->briefNotifications = $user->notifications->filter(function($notification) use($project){
            if($notification->object_type == "brief" || $notification->object_type == "briefversion" && $notification->is_read == 0 && $notification->parent_id == $project->id) {
                $project->calendarAlerts++;
                return $notification;
            }
        });

这会过滤集合并返回一个如下所示的对象

0: {id: 7283, type: "create", activity: "Stephen saved a new version of the brief",…} 1: {id: 7282, type: "create", activity: "Stephen saved a new version of the brief",…} 2: {id: 7281, type: "create", activity: "Stephen saved a new version of the brief",…} 3: {id: 7280, type: "create", activity: "Stephen saved a new version of the brief",…} 5: {id: 7224, type: "update", activity: "Stephen changed Version 2 of the brief",…}

我有一个问题,我需要在我的对象中使用顺序键 - 这可能吗?

我知道如果我的对象是一个数组,那么我就能做到,

array_values($array)

但我知道php中没有类似内容吗?

1 个答案:

答案 0 :(得分:2)

基本上所有array_*函数也可以在Laravel的Collection类中找到,可以找到in the documentation

正如您所说,您通常会使用array_values执行此操作,因此您现在要做的就是通过调用filter将呼叫链接到values

$project->briefNotifications = $user->notifications->filter(...)->values();
相关问题