数组合并递归php

Can anyone tell me how i can convert the first array to second array using php array operations.

First array :-

Array
(
    [0] => Array
        (
            [actual_release_date] => 2013-06-07 00:00:00
            [distributors] => 20th Century Fox / 20th Century Fox Animation / Fox 2000 Pictures / Fox Searchlight
        )

    [1] => Array
        (
            [actual_release_date] => 2013-06-28 11:11:00
            [distributors] => 20th Century Fox / 20th Century Fox Animation / Fox 2000 Pictures / Fox Searchlight
        )
)

Second array:-

Array
(
    [0] => Array
        (
            [actual_release_date] => array( 0=>2013-06-07 00:00:00 , 1=> 2013-06-28 11:11:00 )
            [distributors] => 20th Century Fox / 20th Century Fox Animation / Fox 2000 Pictures / Fox Searchlight
        )
)

If the second element is common, and first element is different, then we have to group it in one array.

Thanks in advance.

You can use array_reduce

$data = array_reduce($data, function ($a, $b) {
    if (isset($a[$b['distributors']])) {
        $a[$b['distributors']]['actual_release_date'][] = $b['actual_release_date'];
    } else {
        $a[$b['distributors']]['actual_release_date'] = array($b['actual_release_date']);
        $a[$b['distributors']]['distributors'] = $b['distributors'];
    }
    return $a;
}, array());

print_r(array_values($data));

Output

Array
(
    [0] => Array
        (
            [actual_release_date] => Array
                (
                    [0] => 2013-06-07 00:00:00
                    [1] => 2013-06-28 11:11:00
                )

            [distributors] => 20th Century Fox / 20th Century Fox Animation / Fox 2000 Pictures / Fox Searchlight
        )

)

See Live DEMO

You try try array marge..

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
//Maerge them
$result = array_merge($array1, $array2);
print_r($result);
?>

The above example will output:

Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

Learn more here

The documentation says:

If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator:

<?php
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$result = $array1 + $array2;
var_dump($result);
?>

The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored.

array(5) {
  [0]=>
  string(6) "zero_a"
  [2]=>
  string(5) "two_a"
  [3]=>
  string(7) "three_a"
  [1]=>
  string(5) "one_b"
  [4]=>
  string(6) "four_b"
}