根据另一个数组的键来减少数组/子数组/对象

时间:2018-06-23 02:46:13

标签: php laravel laravel-collection

我正在尝试基于数组减少对象

请注意,这是一个生成器use Illuminate\Database\Eloquent\Builder;的实例,该实例将与使用vue.js的数据表一起使用。另外请注意,这应该是动态的,我想向getRelationshipValues数组添加一些内容,并根据relationships[]中的列表对其进行过滤。

数组

{  
   data:{  
      table:"minutes",
      records:[  ],
      relationships:[  
         { ... },
         {  
            id:2,
            chapter_id:2,
            brother_id:14,
            started_at:"2008-05-15 22:23:00",
            location:"John's Rotisserie",
            status:"Under Review",
            financial_report:"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus mollis diam dolor, at porttitor libero congue ut. Nunc facilisis enim vitae felis tempus, nec commodo est ornare. Integer auctor, felis nec blandit scelerisque, elit enim luctus libero, blandit ullamcorper orci libero quis quam. Fusce vehicula nulla facilisis quam varius, et vehicula urna malesuada.",
            ended_at:"2008-05-15 22:23:00",
            created_at:"2008-05-15 22:23:00",
            updated_at:"2008-05-15 22:23:00",
            chapter:{  
               id:2,
               name:"Western Islip"
            }
            brother:{
               id:14
               full_name:"Jonathan West"
            }
         },
         { ... }
      ]
   }
}

约束

另一个我想比较键的数组,只从上面的相应键返回它的对应值:

public function getRelationshipValues()
{
    return [
        'chapter' => 'name',
        'brother' => 'full_name'
    ];
}

缩小后的对象应如下所示:

{  
   data:{  
      table:"minutes",
      records:[  ],
      relationships:[  
         { ... },
         {  
            id:2,
            chapter_id:2,
            brother_id:14,
            started_at:"2008-05-15 22:23:00",
            location:"John's Rotisserie",
            status:"Under Review",
            financial_report:"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus mollis diam dolor, at porttitor libero congue ut. Nunc facilisis enim vitae felis tempus, nec commodo est ornare. Integer auctor, felis nec blandit scelerisque, elit enim luctus libero, blandit ullamcorper orci libero quis quam. Fusce vehicula nulla facilisis quam varius, et vehicula urna malesuada.",
            ended_at:"2008-05-15 22:23:00",
            created_at:"2008-05-15 22:23:00",
            updated_at:"2008-05-15 22:23:00",
            chapter:"Western Islip",
            brother:"Jonathan West",
         },
         { ... }
      ]
   }
}

可以使用Laravel的collection方法或PHP的数组函数来完成此操作。我尝试过使用它,但牵引力不足。

编辑

我想根据约束数组中给出的键和值,从Chapter_name和Brother_name中删除ID,并将其转换为类似于第二个关系数组中的字符串

编辑

添加了更多说明,我相信我最初的问题可能使某些内容感到困惑。我们正在尝试遍历relationships数组,仅分别从namefull_name子数组返回chapterbrother

1 个答案:

答案 0 :(得分:0)

您可以将map()的集合方法用作:

$relationships = $relationships->map(function ($relationship) {
    return $relationship->only(['id', 'chapter_id', 'chapter_name.name', ...]);
});

您可以使用Higher Order Messages通过以下方式进一步减少它:

$relationships = $relationships->map->only(['id', 'chapter_id', 'chapter_name.name', ...])

更新

我认为Model的only()方法不适用于 dot.strings (chapter_name.name),因此您可以使用array_only()帮助方法:

$relationships = $relationships->map(function ($relationship) {
    return array_only($relationship, ['id', 'chapter_id', 'chapter_name.name', ...]);
});