将两个数组组合为相同的键和多个值

时间:2018-03-18 10:47:06

标签: php arrays

我有两个阵列,它们具有一个相同的键值对。我想将两个具有相同键值对的数组和其余数组元素组合为多维数组。

以下是数组

阵列1

 $array1=Array
            (
                [0] => Array
                    (
                        [sequence_number] => 2541
                        [name] => Damodhara
                        [connection_date] => 2018-03-16 12:19:41
                        [comm_address] => 
                        [meter_no] => 34347
                        [connection_name] => DOMESTIC
                        [ward_name] => ALAPE
                        [corp_name] => Corp1
                        [agent_name] => agent
                    )

                [1] => Array
                    (
                        [sequence_number] => 4568
                        [name] => Das
                        [connection_date] => 2018-03-15 13:38:49
                        [comm_address] => 
                        [meter_no] => 108
                        [connection_name] => DOMESTIC-FLAT
                        [ward_name] => ALAPE
                        [corp_name] => Corp1
                        [agent_name] => mcc
                    )

                [2] => Array
                    (
                        [sequence_number] => 7890
                        [name] => Reghu
                        [connection_date] => 2018-03-17 11:18:32
                        [comm_address] => 
                        [meter_no] => 35345
                        [connection_name] => DOMESTIC
                        [ward_name] => ALAPE
                        [corp_name] => Corp1
                        [agent_name] => 
                    )

                [3] => Array
                    (
                        [sequence_number] => 12345
                        [name] => Abhay
                        [connection_date] => 2018-03-15 13:38:44
                        [comm_address] => 
                        [meter_no] => 666
                        [connection_name] => DOMESTIC
                        [ward_name] => ALAPE
                        [corp_name] => Corp1
                        [agent_name] => agent
                    )

            )

数组2

        $array2=Array
        (
            [0] => Array
                (
                    [sequence_number] => 4568
                    [date_of_reading] => 2018-03-16 09:25:38
                    [previous_billing_date] => 0000-00-00 00:00:00
                    [previous_reading] => 1000
                    [current_reading] => 1200
                    [water_charge] => 100.00
                    [supervisor_charge] => 0.00
                    [penalty] => 0.00
                    [meter_status] => NOT LEGIBLE
                    [bill_no] => 1678
                    [arrears] => 0.00
                    [total_amount] => 100.00
                    [total_unit_used] => 890
                    [paid_amount] => 100
                    [payment_date] => 2018-02-26 00:00:00
                    [transaction_number] => 
                )

            [1] => Array
                (
                    [sequence_number] => 4568
                    [date_of_reading] => 2018-03-16 09:25:38
                    [previous_billing_date] => 2018-02-12 00:00:00
                    [previous_reading] => 1000
                    [current_reading] => 1200
                    [water_charge] => 100.00
                    [supervisor_charge] => 0.00
                    [penalty] => 0.00
                    [meter_status] => MNR
                    [bill_no] => 1690
                    [arrears] => 0.00
                    [total_amount] => 250.00
                    [total_unit_used] => 0
                    [paid_amount] => 250
                    [payment_date] => 2018-03-05 00:00:00
                    [transaction_number] => 
                )

            [2] => Array
                (
                    [sequence_number] => 12345
                    [date_of_reading] => 2018-03-15 12:43:58
                    [previous_billing_date] => 2018-02-13 00:00:00
                    [previous_reading] => 2000
                    [current_reading] => 2200
                    [water_charge] => 100.00
                    [supervisor_charge] => 0.00
                    [penalty] => 0.00
                    [meter_status] => NOT LEGIBLE
                    [bill_no] => 1777
                    [arrears] => 0.00
                    [total_amount] => 200.00
                    [total_unit_used] => 200
                    [paid_amount] => 
                    [payment_date] => 
                    [transaction_number] => 
                )


            [3] => Array
                (
                    [sequence_number] => 2541
                    [date_of_reading] => 2018-03-13 10:26:05
                    [previous_billing_date] => 2017-11-01 15:24:29
                    [previous_reading] => 10000
                    [current_reading] => 11000
                    [water_charge] => 0.00
                    [supervisor_charge] => 0.00
                    [penalty] => 0.00
                    [meter_status] => MNR
                    [bill_no] => 446986171123632
                    [arrears] => 0.00
                    [total_amount] => 0.00
                    [total_unit_used] => 1000
                    [paid_amount] => 0
                    [payment_date] => 2018-03-05 00:00:00
                    [transaction_number] => 
                )


            [4] => Array
                (
                    [sequence_number] => 2541
                    [date_of_reading] => 2018-03-16 11:41:47
                    [previous_billing_date] => 2018-03-13 10:26:05
                    [previous_reading] => 10000
                    [current_reading] => 12000
                    [water_charge] => 6.50
                    [supervisor_charge] => 0.00
                    [penalty] => 0.00
                    [meter_status] => ABNORMAL READING
                    [bill_no] => 843227171127296
                    [arrears] => 0.00
                    [total_amount] => 17.00
                    [total_unit_used] => 2000
                    [paid_amount] => 7
                    [payment_date] => 2018-03-06 00:00:00
                    [transaction_number] => 
                )


        )

需要的新数组,其字段值(读取到事务编号的日期)来自相同序列号的第二个数组,作为新数组中的单独数组。该数组中的多维数组。

2 个答案:

答案 0 :(得分:0)

您可以使用array_merge() 函数

合并数组

答案 1 :(得分:0)

这是猜测......

将array2嵌入array1'children'索引......

// index array 1 by sequence_number
$array1 = array_combine( array_column( $array1, 'sequence_number' ), $array1 );

foreach( $array2 as $two )
{
  if( !isset( $array1[ $two['sequence_number'] ]['children'] ) )
  {
    $array1[ $two['sequence_number'] ]['children'] = array();
  }
  $array1[ $two['sequence_number'] ]['children'][] = $two;
}