简体   繁体   English

如何通过键转换Laravel集合?

[英]How to transform a Laravel collection by keys?

I have a Laravel collection where I have data for users (their posts, comments and reputation). 我有一个Laravel集合,其中有用户数据(他们的帖子,评论和声誉)。 I would like to transform this collection where each item becomes one of the three properties (posts, comments and reputation). 我想对这个集合进行改造,使每个项目成为三个属性(帖子,评论和声誉)之一。

So the result would be like - for field: 'posts' and user_id: 46 we have 1542 posts. 因此结果就像-对于字段:“ posts”和u​​ser_id:46,我们有1542个帖子。 And for field: 'comments' the same user has 2. etc. 对于字段:“评论”,同一用户有2,等等。

I tried to create multiple loops over the collection to get to this result but it is not working as I don't know how may loops I need. 我试图在集合上创建多个循环以获取此结果,但由于我不知道我需要的可能是怎样的循环而无法正常工作。 Is there a better and more clean way to do this? 有没有更好,更干净的方法来做到这一点?

Here is my original collection 这是我的原始收藏

"data": [
    {
        "user_id": 46,
        "username": "johnive",
        "posts": 1542,
        "comments": 2,
        "reputation": 48.5,
    },

    {
        "user_id": 30,
        "username": "zacky13",
        "posts": 54,
        "comments": 16,
        "reputation": 14.3,
    },

    {
        "user_id": 107,
        "username": "lil_elf4",
        "posts": 564,
        "comments": 60,
        "reputation": 67.5,
    },
],

Here is the result I would like to achieve 这是我想要实现的结果

"data": [
    {
        "user_id": 46,
        "username": "johnive",
        "field": "posts",
        "value": 1542
    },

    {
        "user_id": 46,
        "username": "johnive",
        "field": "comments",
        "value": 2
    },

    {
        "user_id": 46,
        "username": "johnive",
        "field": "reputation",
        "value": 48.5
    },

    {
        "user_id": 30,
        "username": "zacky13",
        "field": "posts",
        "value": 54
    },

    {
        "user_id": 30,
        "username": "zacky13",
        "field": "comments",
        "value": 16
    },

    ...
],

I tried using multiple loops seems like a dirty way to do it and I don't know how many loops I would need. 我尝试使用多个循环似乎是一种肮脏的方式,但我不知道我需要多少个循环。

foreach ($collection as $item)
{
  $new_item = new stdClass;

  $new_item->user_id = $item->user_id;
  $new_item->username = $item->username;
  $new_item->field = 'posts';
  $new_item->value = $item->posts;
}

foreach ($collection as $item)
{
  $new_item = new stdClass;

  $new_item->user_id = $item->user_id;
  $new_item->username = $item->username;
  $new_item->field = 'comments';
  $new_item->value = $item->comments;
}

foreach ($collection as $item)
{
  $new_item = new stdClass;

  $new_item->user_id = $item->user_id;
  $new_item->username = $item->username;
  $new_item->field = 'reputation';
  $new_item->value = $item->reputation;
}

You could do it eg like this: 您可以这样做,例如:

$result = collect();
foreach ($collection as $item)
{
  foreach(['posts', 'comments', 'reputations'] as $type) {
    $new_item = new stdClass;
    $new_item->user_id = $item->user_id;
    $new_item->username = $item->username;
    $new_item->field = $type;
    $new_item->value = $item->$type;
    $result->push($new_item);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM