Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何通过关键点变换Laravel集合?_Php_Arrays_Laravel_Collections - Fatal编程技术网

Php 如何通过关键点变换Laravel集合?

Php 如何通过关键点变换Laravel集合?,php,arrays,laravel,collections,Php,Arrays,Laravel,Collections,我有一个Laravel收集,其中我有用户的数据(他们的帖子、评论和声誉)。我想改变这个集合,使每个项目成为三个属性之一(帖子、评论和声誉) 因此,结果将类似于-对于字段:“posts”和用户id:46,我们有1542篇文章。对于字段:“评论”,同一用户有2个。等等 我试图在集合上创建多个循环以获得此结果,但它不起作用,因为我不知道我需要如何创建循环。有没有更好更干净的方法 这是我的原始收藏 "data": [ { "user_id": 46, "usern

我有一个Laravel收集,其中我有用户的数据(他们的帖子、评论和声誉)。我想改变这个集合,使每个项目成为三个属性之一(帖子、评论和声誉)

因此,结果将类似于-对于字段:“posts”和用户id:46,我们有1542篇文章。对于字段:“评论”,同一用户有2个。等等

我试图在集合上创建多个循环以获得此结果,但它不起作用,因为我不知道我需要如何创建循环。有没有更好更干净的方法

这是我的原始收藏

"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,
    },
],
以下是我想要实现的结果

"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
    },

    ...
],
我尝试使用多个循环似乎是一种肮脏的方法,我不知道需要多少个循环

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

你可以这样做,例如:

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