在PHP中搜索数组并创建自定义数组

在PHP中搜索数组并创建自定义数组,php,arrays,function,Php,Arrays,Function,我在合并数组时遇到问题。我有3个数组显示特定的数据。我需要一种方法来搜索所有三个数组,如果记录与日期匹配,我需要将其存储在一个新的自定义数组中。 我有3个数组需要使用日期合并它们 数组如下所示 // ARRAY 1 $transactions = array( array( "date"=>"2021-03-01", "trans_count"=>100, "trans_amount"=

我在合并数组时遇到问题。我有3个数组显示特定的数据。我需要一种方法来搜索所有三个数组,如果记录与日期匹配,我需要将其存储在一个新的自定义数组中。 我有3个数组需要使用日期合并它们 数组如下所示

// ARRAY 1
$transactions = array(
array(
   "date"=>"2021-03-01",
   "trans_count"=>100,
   "trans_amount"=>5300
),
array(
   "date"=>"2021-03-02",
   "trans_count"=>95,
   "trans_amount"=>5035
),
array(
   "date"=>"2021-03-03",
   "trans_count"=>105,
   "trans_amount"=>5565
)
);

// ARRAY 2
$overdrafts = array(
array(
   "date"=>"2021-03-01",
   "od_amount"=>500
),
array(
   "date"=>"2021-03-02",
   "od_amount"=>1000
),
);

// ARRAY 3
$payouts= array(
array(
   "date"=>"2021-03-02",
   "amount"=>2300
)
);
我试图写一个函数,但我被卡住了。最终目标是从所有3个数组中收集所有记录并生成一个数组。 预期的结果应该是这样的

$merged_arr = array(

   array(
       "date"=>"2021-03-01",
       "type"=>"transactions",
       "trans_count"=>100,
       "trans_amount"=>5300
    ),
   array(
       "date"=>"2021-03-01",
       "type"=>"overdrafts",
       "od_amount"=>500,
       
    ),

   array(
       "date"=>"2021-03-02",
       "type"=>"transactions",
       "trans_count"=>95,
       "trans_amount"=>5035
    ),
   array(
       "date"=>"2021-03-02",
       "type"=>"overdrafts",
       "od_amount"=>1000
    ),
   array(
       "date"=>"2021-03-02",
       "type"=>"payout",
       "od_amount"=>2300
    ),
);

无法100%确定如何以一种好的方式为每个不同的类型自动添加
类型
,我建议直接在初始数组中添加类型,或者在合并它们之前循环每个类型

至于合并和排序,它非常简单,可以通过内置功能完成

$merged\u arr=array\u merge($transactions、$overdrafts、$payouts);
usort($merged_arr,function($left,$right){
返回新的日期时间($left['date'])新的日期时间($right['date']);
});
//演示合并阵列。
打印“”;
var_出口($merged_arr);
排序函数将与PHP7及更高版本一起使用