如何通过深度键组合两个数组?

How to combine two arrays by deep key?

我想使用指定的深子数组值组合两个数组。

我有两个具有不同结构的不同数组,我想将它们组合起来,以便如果 "primary keys" 匹配,则将第二个数组的值添加到第一个数组,如果不匹配,则使用数组 2 的值创建数组。

第一个数组中的主键是 [created_by],第二个数组中的主键是 [upgrade_by]

Array1 是:

Array(      
        [0] => Array(
                [Customer] => Array(
                        [created_by] => 5
                        [amount] => 199
                        [name] => First Cux
                )
        )
        [1] => Array(
                [Customer] => Array(
                        [created_by] => 1
                        [amount] => 199
                        [name] => Last Cux
                )
        )
)

数组 2 是

Array(
        [0] => Array(
                [0] => Array(
                        [refund_amount] => 100
                )
                [Historycustomer] => Array(
                        [upgrade_by] => 1
                        [company] => First Company
                )
        )
        [1] => Array(
                [0] => Array(
                        [refund_amount] => 250
                )
                [Historycustomer] => Array(
                        [upgrade_by] => 3
                        [company] => Last Company
                )
        )
)

我需要这样的结果:

Array(      
        [0] => Array(
                [Customer] => Array(
                        [created_by] => 5
                        [amount] => 199
                        [name] => First Cux
                )
        )
        [1] => Array(
                [Customer] => Array(
                        [created_by] => 1
                        [amount] => 199
                        [refund_mount]=>100
                        [name] => Last Cux
                        [company] => First Company
                )
        )
        [2] => Array(
                [Customer] => Array(
                        [created_by] => 3
                        [refund_mount]=>250
                        [company] => Last Company
                )
        )
)

第一个数组中的主键是 [created_by],第二个数组中的主键是 [upgrade_by]

我的方法将使用输入数组 $a 作为结果数组。 foreach() 将遍历 $b 的子数组。 $index$a 中的子数组的键在其 created_by 值中包含 upgrade_by 值)通过以下方式找到:

  1. 使用 array_column() 隔离 $aCustomer "columns" 中的最深子数组——这些是 3 元素数组。
  2. array_column() 再次用于隔离这些子数组中的 created_by 值。
  3. array_search() 在新的独立数组中寻找指定的数字,如果它存在,returns 它的索引。

找到索引后,$b 的值将使用给定索引与 $a 的值合并,并省略 upgrade_by 元素。

当找不到索引时,upgrade_by 被重命名为 created_by 并将元素添加到 $a 作为新的客户子数组。

代码:(Demo Link)

$a=[
    ['Customer'=>['created_by'=>5,'amount'=>199,'name'=>'First Cux']],
    ['Customer'=>['created_by'=>1,'amount'=>199,'name'=>'Last Cux']]
];
$b=[
    [['refund_amount'=>100],'Historycustomer'=>['upgrade_by'=>1,'company'=>'First Company']],
    [['refund_amount'=>250],'Historycustomer'=>['upgrade_by'=>3,'company'=>'Last Company']]
];
$created_by_indexes=array_column(array_column($a,'Customer'),'created_by');
foreach($b as $b1){
    $index=array_search($b1['Historycustomer']['upgrade_by'],$created_by_indexes);
    if($index!==false){
        $a[$index]['Customer']+=['refund_amount'=>$b1[0]['refund_amount'],'company'=>$b1['Historycustomer']['company']];
    }else{
        $a[]=['Customer'=>['created_by'=>$b1['Historycustomer']['upgrade_by'],'refund_amount'=>$b1[0]['refund_amount'],'company'=>$b1['Historycustomer']['company']]];
    }
}
var_export($a);

输出:

array (
  0 => 
  array (
    'Customer' => 
    array (
      'created_by' => 5,
      'amount' => 199,
      'name' => 'First Cux',
    ),
  ),
  1 => 
  array (
    'Customer' => 
    array (
      'created_by' => 1,
      'amount' => 199,
      'name' => 'Last Cux',
      'refund_amount' => 100,
      'company' => 'First Company',
    ),
  ),
  2 => 
  array (
    'Customer' => 
    array (
      'created_by' => 3,
      'refund_amount' => 250,
      'company' => 'Last Company',
    ),
  ),
)