我有一个类似的数组,如下所示:

       $agents = [
          (object)["id"=> 301,"name"=> "Agent 1"],
           (object)["id"=> 303,"name"=> "Agent 2"],
           (object)["id"=> 301,"name"=> "Agent 3"],
           (object)["id"=> 305,"name"=> "Agent 4"],
           (object)["id"=> 306,"name"=> "Agent 5"],
           (object)["id"=> 303,"name"=> "Agent 6"]
        ];


         $locations =  [
             (object)["xid"=> 148,"location_name"=> "USA"],
             (object)["xid"=> 149,"location_name"=> "CHINA"],
             (object)["xid"=> 153,"location_name"=> "GERMANY"],
             (object)["xid"=> 156,"location_name"=> "CANADA"],
             (object)["xid"=> 167,"location_name"=> "SINGAPORE"],
             (object)["xid"=> 0,"location_name"=> "No Location"]
        ];

       
        $records = [
           (object)["agent_id"=> 301,"location_id"=> 156,"total"=> 10000],
           (object)["agent_id"=> 303,"location_id"=> 149,"total"=> 13000],
           (object)["agent_id"=> 301,"location_id"=> 156,"total"=> 10000],
           (object)["agent_id"=> 305,"location_id"=> 0,"total"=> 10000],
           (object)["agent_id"=> 306,"location_id"=> 148,"total"=> 10000],
           (object)["agent_id"=> 303,"location_id"=> 0,"total"=> 10000]
        ];

我正在try 将正确的事务分配到$AGENT-&>LOCATIONS->事务中,但我的循环不起作用.

        foreach ($agents as $a) {
            foreach ($locations as $l) {
                $a->locations[]=$l;
                foreach ($records as $r) {
                    if($r->location_id == $l->xid && $r->agent_id == $a->id){
                        $l->transactions[] = $r;
                    }
                }
            }
        }

这就是结果

enter image description here

我想要实现的是,事务应该在正确的代理下分配/过滤.请帮帮我..我已经关注这个循环太久了,不知道如何实现我需要的:(.

enter image description here

推荐答案

You are dealing with objects.所以,对它所做的任何修改都会在以后反映出来.

你做了$l->transactions[] = $r;次.因此,现在$l对象也将具有以前的事务详细信息.

要解决此问题,您可以创建对象的快速克隆,并使用clone修改此新对象.

Snippet:

<?php

foreach($agents as $agent){
    $agent->locations = [];
    foreach($locations as $location){
        $location = clone $location;
        $location->transactions = [];
        foreach($records as $r){
            if($r->agent_id == $agent->id && $location->xid == $r->location_id){
                $location->transactions[] = $r;
            }
        }
        
        $agent->locations[] = $location;
    }
}

Live Demo

Php相关问答推荐

自定义通知在Woocommerce我的帐户自定义端点页面上不起作用

过滤WooCommerce管理员订单列表(+HPOS)中包含其作者产品的订单

如何减少Laravel分页中的链接数

在Laravel Eloquent中实现一对多关系

如何在laravel中添加延迟?

为什么WooCommerce的动作钩子';woocommerce_checkout_update_order_review';包含旧信息?

WooCommerce中特定付款方式的变更订单预留库存时间

将数据推入所有子数组条目

将自定义字段添加到管理产品中 WooCommerce 3.2+ 中的快速编辑

将自定义字段添加到管理产品 在 WooCommerce 3.2+ 中批量编辑

Google Drive API 服务->文件->get('root') 失败并显示找不到文件. (范围问题?)

使用 foreach php 将记录正确分配给正确的父级

在 PHP 中验证签名

Laravel:ArgumentCountError: 函数 App\Mail 的参数太少

为什么在解压缩具有混合字符串和 int 键的关联数组时出现错误消息无法使用位置参数...?

laravel 中的 OrderBy json 列

如何从 PHP 中的字符串位置排除图像扩展名

来自命令行的 PHP 没有给出正确的结果

Symfony 自定义注销

使用 PHP 有条件地使用关键字 Twilio Programmable SMS