使用Eloquent在PHP中合并数组的对象
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用Eloquent在PHP中合并数组的对象,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4048字,纯文字阅读大概需要6分钟。
内容图文
![使用Eloquent在PHP中合并数组的对象](/upload/InfoBanner/zyjiaocheng/774/6a8a3ce7e0314f459bd7ba329f64717f.jpg)
我使用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”}值组合成一个对象 – 将它们的数量和价格属性加在一起,使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);
内容总结
以上是互联网集市为您收集整理的使用Eloquent在PHP中合并数组的对象全部内容,希望文章能够帮你解决使用Eloquent在PHP中合并数组的对象所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。