使用列作为参考(php,laravel)添加值​从集合到其他集合

发布于 2025-02-12 05:22:22 字数 887 浏览 0 评论 0原文

我正在与Laravel的PHP合作,在这种情况下,我有2个对象集合,其中一个看起来像这样:

[
    {
        "id": 1,
        "name": "product x",
        "quantity": "100",
    },
    {
        "id": 2,
        "codProd": "product y",
        "quantity": "200",
    },
    {
        "id": 3,
        "name": "product a.",
        "quantity": "30",
    }
]

另一个看起来像这样:

[
    {
        "reference": 1,
        "quantity": "80",
    },
    {
        "reference": 2,
        "quantity": "50",
    },
]

我需要保留第一个集合,但要从中添加数量密钥的值第二个集合使用参考密钥作为与第一个集合的ID的关系,最终结果应该看起来像这样:

[
    {
        "id": 1,
        "name": "product x",
        "quantity": "180",
    },
    {
        "id": 2,
        "codProd": "product y",
        "quantity": "250",
    },
    {
        "id": 3,
        "name": "product a.",
        "quantity": "30",
    }
]

我该怎么做?

I am working with php in laravel, in this case I have 2 collections of objects, one of them looks like this:

[
    {
        "id": 1,
        "name": "product x",
        "quantity": "100",
    },
    {
        "id": 2,
        "codProd": "product y",
        "quantity": "200",
    },
    {
        "id": 3,
        "name": "product a.",
        "quantity": "30",
    }
]

and the other looks like this:

[
    {
        "reference": 1,
        "quantity": "80",
    },
    {
        "reference": 2,
        "quantity": "50",
    },
]

What I need is to keep the first collection but adding the value of the quantity key from the second collection, using the reference key as a relationship with the id of the first collection, the final result should look like this:

[
    {
        "id": 1,
        "name": "product x",
        "quantity": "180",
    },
    {
        "id": 2,
        "codProd": "product y",
        "quantity": "250",
    },
    {
        "id": 3,
        "name": "product a.",
        "quantity": "30",
    }
]

so how can I do this?, any guide or help I am grateful

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追星践月 2025-02-19 05:22:22

您可以为此使用收集方法,即地图方法:

$collection_one = collect([
    [
        'id' => 1,
        'name' => 'product x',
        'quantity' => 100,
    ],
    [
        'id' => 2,
        'name' => 'product y',
        'quantity' => 100,
    ],
    [
        'id' => 1,
        'name' => 'product a.',
        'quantity' => 30,
    ],
]);

$collection_two = collect([
    [
        'reference' => 1,
        'quantity' => 80,
    ],
    [
        'reference' => 2,
        'quantity' => 50,
    ],
]);

让您获得收藏的内容:

$collect = [];

$collect = $collection_one->map(function ($value_one) use ($collection_two) {
    if ($search = $collection_two->firstWhere('reference', '=', $value_one['id'])) {
        $value_one['quantity'] += $search['quantity'];
    }

    return $value_one;
});

准备好,对我有用。 :)

You can use a collection method for this, the map method:

$collection_one = collect([
    [
        'id' => 1,
        'name' => 'product x',
        'quantity' => 100,
    ],
    [
        'id' => 2,
        'name' => 'product y',
        'quantity' => 100,
    ],
    [
        'id' => 1,
        'name' => 'product a.',
        'quantity' => 30,
    ],
]);

$collection_two = collect([
    [
        'reference' => 1,
        'quantity' => 80,
    ],
    [
        'reference' => 2,
        'quantity' => 50,
    ],
]);

For you to get the collection what you want:

$collect = [];

$collect = $collection_one->map(function ($value_one) use ($collection_two) {
    if ($search = $collection_two->firstWhere('reference', '=', $value_one['id'])) {
        $value_one['quantity'] += $search['quantity'];
    }

    return $value_one;
});

Ready, that worked for me. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文