php - 使用 Eloquent 在 PHP 中合并数组对象

标签 php arrays laravel merge eloquent

我使用 Laravel 的 Eloquent ORM 从我的数据库中抓取了一组对象(我没有使用 Laravel,只是将 Eloquent 集成到我自己的框架中)。我用了transform方法遍历集合并反序列化每条记录的列之一,然后是collapsed将所有未序列化的对象放在一个数组中的集合。

逻辑如下:

  $orders = Order::where('user', $user)->orderBy('id', 'desc')->get();

  $orders->transform(function($order, $key) {

    $order->cart = unserialize($order->cart);

    $items = $order->cart->items;

    return $items;
  });

  $collapsed = $orders->collapse();

和输出:

[
   {
      "qty": "2",
      "price": 200,
      "item": {
         "id": 1,
         "title": "Black Hoodie",
         "img": "https://s3.amazonaws.com/bucket/black-hoodie.jpg",
         "description": "Plain Black Hoodie.",
         "price": 100
      }
   },
   {
      "qty": "2",
      "price": 200,
      "item": {
         "id": 2,
         "title": "Green Hoodie",
         "img": "https://s3.amazonaws.com/bucket/green-hoodie.jpg",
         "description": "Plain Green Hoodie.",
         "price": 100
      }
   },
   {
      "qty": 1,
      "price": 100,
      "item": {
         "id": 2,
         "title": "Green Hoodie",
         "img": "https://s3.amazonaws.com/bucket/green-hoodie.jpg",
         "description": "Plain Green Hoodie.",
         "price": 100
      }
   },
   {
      "qty": 1,
      "price": 100,
      "item": {
         "id": 1,
         "title": "Black Hoodie",
         "img": "https://s3.amazonaws.com/bucket/black-hoodie.jpg",
         "description": "Plain Black Hoodie.",
         "price": 100
      }
   }
]

现在我接下来要完成的是将此数组中所有相同的对象(最好是通过它们的 "item":{"id"} 值组合到一个对象中 - 添加它们的 qtyprice 属性放在一起,item 属性保持不变。

我想要的输出是

[
   {
      "qty": "3",
      "price": 300,
      "item": {
         "id": 1,
         "title": "Black Hoodie",
         "img": "https://s3.amazonaws.com/bucket/black-hoodie.jpg",
         "description": "Plain Black Hoodie.",
         "price": 100
      }
   },
   {
      "qty": "3",
      "price": 300,
      "item": {
         "id": 2,
         "title": "Green Hoodie",
         "img": "https://s3.amazonaws.com/bucket/green-hoodie.jpg",
         "description": "Plain Green Hoodie.",
         "price": 100
      }
   }
]

Eloquent 有 TONS有很多可用于处理集合的好方法,我只是坚持使用正确的组合来有效地实现我想做的事情。

最佳答案

试试这个:

use Illuminate\Support\Fluent;
use Illuminate\Support\Collection;

// Mocking data to meet your requirements.
// Wrapping attributes inside fluent object,
// so we'll have almost same object like Eloquent model
$orders = new Collection([
    new Fluent([
        'qty' => 2,
        'price' => 200,
        'item' => new Fluent([
            'id' => 1,
            'price' => 100,
            'title' => 'Black Hoodie',
            'img' => 'https://s3.amazonaws.com/bucket/black-hoodie.jpg',
            'description' => 'Plain Black Hoodie.',
        ])
    ]),
    new Fluent([
        'qty' => 2,
        'price' => 200,
        'item' => new Fluent([
            'id' => 2,
            'price' => 100,
            'title' => 'Green Hoodie',
            'img' => 'https://s3.amazonaws.com/bucket/green-hoodie.jpg',
            'description' => 'Plain Green Hoodie.',
        ])
    ]),
    new Fluent([
        'qty' => 1,
        'price' => 100,
        'item' => new Fluent([
            'id' => 2,
            'price' => 100,
            'title' => 'Green Hoodie',
            'img' => 'https://s3.amazonaws.com/bucket/green-hoodie.jpg',
            'description' => 'Plain Green Hoodie.',
        ])
    ]),
    new Fluent([
        'qty' => 1,
        'price' => 100,
        'item' => new Fluent([
            'id' => 1,
            'price' => 100,
            'title' => 'Black Hoodie',
            'img' => 'https://s3.amazonaws.com/bucket/black-hoodie.jpg',
            'description' => 'Plain Black Hoodie.',
        ])
    ])
]);

// Here's something you might interested
$result = $orders->groupBy('item.id')
    ->map(function ($orders, $itemId) {
        return new Fluent([
            'qty' => $orders->sum('qty'), // calculating quantity
            'price' => $orders->sum('price'), // calculating total price
            'item' => $orders->first()->item // leave the item as it is
        ]);
    });

// You may evaluate the result here
dd($result);

更新

更多“原始”示例:

use Illuminate\Support\Collection;

$orderOne = [
    'id' => 1,
    'price' => 100,
    'title' => 'Black Hoodie',
    'img' => 'https://s3.amazonaws.com/bucket/black-hoodie.jpg',
    'description' => 'Plain Black Hoodie.',
];

$orderTwo = [
    'id' => 2,
    'price' => 100,
    'title' => 'Green Hoodie',
    'img' => 'https://s3.amazonaws.com/bucket/green-hoodie.jpg',
    'description' => 'Plain Green Hoodie.',
];

$orders = new Collection([
    [
        'qty' => 2,
        'price' => 200,
        'item' => $orderOne
    ],
    [
        'qty' => 2,
        'price' => 200,
        'item' => $orderTwo
    ],
    [
        'qty' => 1,
        'price' => 100,
        'item' => $orderTwo
    ],
    [
        'qty' => 1,
        'price' => 100,
        'item' => $orderOne
    ]
]);

$result = $orders->groupBy('item.id')
    ->map(function ($orders, $itemId) {
        return [
            'qty' => $orders->sum('qty'),
            'price' => $orders->sum('price'),
            'item' => $orders->first()['item']
        ];
    });

dd($result);

关于php - 使用 Eloquent 在 PHP 中合并数组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45584164/

相关文章:

php - fsockopen PHP检查RDP的打开端口不起作用

css - Laravel v5.2 分页显示

php - 生成要存储在数据库中的唯一随机数

php - 将特色图片添加到自定义帖子类型

javascript - 将新创建的 dom 元素添加到一个空的 jQuery 对象

php - 数组中的两个 PHP 值

php - 如何以 Laravel 方式执行此连接查询

javascript - 如何处理 php (Laravel api) 和 javascript (AngularJS) 之间的日期时间

php - 为什么这个简单的 "INSERT INTO"查询不起作用

javascript - 当键是动态的时,按值对对象数组进行排序