merging two multidimentional array with same value merge with another array php

Viewed 20

I have two array and want it to merge in one with first array key value match to another than the value of first array goes to second array key value and give me result like mention below.

this is array one

Array

[0] => Array
    (
        [view_date] => 2022-09-10
        [t_view] => 1
    )

[1] => Array
    (
        [view_date] => 2022-09-11
        [t_view] => 19
    )

)

this is array two

Array
(
[0] => Array
    (
        [view_date] => 2022-09-05
        [t_view] => 0
    )

[1] => Array
    (
        [view_date] => 2022-09-06
        [t_view] => 0
    )

[2] => Array
    (
        [view_date] => 2022-09-07
        [t_view] => 0
    )

[3] => Array
    (
        [view_date] => 2022-09-08
        [t_view] => 0
    )

[4] => Array
    (
        [view_date] => 2022-09-09
        [t_view] => 0
    )

[5] => Array
    (
        [view_date] => 2022-09-10
        [t_view] => 0
    )

[6] => Array
    (
        [view_date] => 2022-09-11
        [t_view] => 0
    )

)

wanted result like this, with same length and same key but value of match date is placed in second date key.

  Array
  (
   [0] => Array
    (
        [view_date] => 2022-09-05
        [t_view] => 0
    )

[1] => Array
    (
        [view_date] => 2022-09-06
        [t_view] => 0
    )

[2] => Array
    (
        [view_date] => 2022-09-07
        [t_view] => 0
    )

[3] => Array
    (
        [view_date] => 2022-09-08
        [t_view] => 0
    )

[4] => Array
    (
        [view_date] => 2022-09-09
        [t_view] => 0
    )

[5] => Array
    (
        [view_date] => 2022-09-10
        [t_view] => 1
    )

[6] => Array
    (
        [view_date] => 2022-09-11
        [t_view] => 19
    )

)

I am stuck please help me. thanks

1 Answers

You can follow this solution like this:

foreach($arr2 as $key => $value2){
foreach($arr1 as $value1){
    if($value2['view_date'] === $value1['view_date']){
        $arr2[$key]['t_view'] = $value1['t_view'];
    }
}

}

Related