-2

I need to merge 3 dimentional array with php and need result. Tell me how to achieve this. Below is my two array output. Is there any inbuilt function available for this ?

Array1

Array
(
    [4af2986d-92d6-f343-4c55-5da954718c48] => Array
        (
            [2020-01-10] => Array
                (
                    [10] => Array
                        (
                            [user_name] => dreamertechs
                            [Call_Answered] => 0
                            [Call_Dropes] => 1
                            [call_duration_seconds] => 0
                        )                    

                    [11] => Array
                        (
                            [user_name] => dreamertechs
                            [Call_Answered] => 2
                            [Call_Dropes] => 1
                            [call_duration_seconds] => 91
                        )

                )
        )
)

Array2

Array
(
    [4af2986d-92d6-f343-4c55-5da954718c48] => Array
        (
            [2020-01-10] => Array
                (
                    [10] => Array
                        (
                            [user_name] => dreamertechs
                            [break_duration] => 19
                        )

                    [16] => Array
                        (
                            [user_name] => dreamertechs
                            [break_duration] => 1
                        )
                )
        )
)

Result_array

Array
(
    [4af2986d-92d6-f343-4c55-5da954718c48] => Array
        (
            [2020-01-10] => Array
                (
                    [10] => Array
                        (
                            [user_name] => dreamertechs
                            [Call_Answered] => 0
                            [Call_Dropes] => 1
                            [call_duration_seconds] => 0
                            [break_duration] => 19
                        )                    

                    [11] => Array
                        (
                            [user_name] => dreamertechs
                            [Call_Answered] => 2
                            [Call_Dropes] => 1
                            [call_duration_seconds] => 91
                        )
                    [16] => Array
                        (
                            [user_name] => dreamertechs
                            [break_duration] => 1
                        )
                )
        )
)

I have used beow function but not get the aspected result.

$array = array_unique (array_merge ($call_data, $break_data));
LOKESH
  • 1,303
  • 1
  • 16
  • 29
  • In the future, please 1. exhaustively research (because nearly every basic data manipulation is already provided in the millions of pages created over the last 10 years 2. Show you array data as `var_export()` or a json_encoded string instead of `print_r()` or `var_dump()` -- this allows volunteers to instantly use your exact data in their answers. – mickmackusa Jan 12 '20 at 20:27

1 Answers1

-1

array_replace_recursive will do here:

<?php

$one =
[
    'a1' =>  [
        '2010' => [
            1 => [
                'name' => 'Ringo'
            ],
            2 => [
                'name' => 'Paul'
            ]
        ]
    ]
];
$two =
[
    'a1' =>  [
        '2010' => [
            1 => [
                'name' => 'Ringo',
                'instrument' => 'Drums'
            ],
            3 => [
                'name' => 'George'
            ]
        ]
    ]
];

var_export(array_replace_recursive($two, $one));

Output:

array (
  'a1' => 
  array (
    2010 => 
    array (
      1 => 
      array (
        'name' => 'Ringo',
        'instrument' => 'Drums',
      ),
      3 => 
      array (
        'name' => 'George',
      ),
      2 => 
      array (
        'name' => 'Paul',
      ),
    ),
  ),
)
Progrock
  • 7,373
  • 1
  • 19
  • 25