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

时间:2017-08-09 07:26:12

标签: php arrays laravel merge eloquent

我使用Laravel的Eloquent ORM从我的数据库中抓取了一组对象(我没有使用Laravel将Eloquent集成到我自己的框架中)。我使用https://stackoverflow.com/a/5344074/501217方法迭代集合和unserialize每个记录的一列,然后transform集合将所有未序列化的对象放在一个数组中。

以下是逻辑:

  $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有collapsed个可用于处理集合的好方法,我只是坚持正确的组合以有效地实现我想要做的事情。

1 个答案:

答案 0 :(得分:1)

试试这个:

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

更新

更多" raw"例如:

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