0

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

我有两个具有不同结构的不同数组,我想将它们组合起来,以便如果“主键”匹配,则将第二个数组的值添加到第一个数组,如果不匹配,则使用数组 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]

4

1 回答 1

1

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

  1. 使用- 这些是 3 元素数组来隔离$a' 的Customer“列”中最深的子数组。array_column()
  2. array_column()再次用于隔离created_by这些子数组中的值。
  3. array_search()在新的孤立数组中寻找指定的数字,如果存在,则返回其索引。

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

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

代码:(演示链接

$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',
    ),
  ),
)
于 2017-07-02T11:32:03.937 回答