【问题标题】:Array merge recursively php数组递归合并php
【发布时间】:2013-06-13 09:32:17
【问题描述】:

谁能告诉我如何使用 php 数组操作将第一个数组转换为第二个数组。

第一个数组:-

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
        )
)

第二个数组:-

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
        )
)

如果第二个元素是共同的,而第一个元素是不同的,那么我们必须将它分组到一个数组中。

提前致谢。

【问题讨论】:

  • 你尝试过简单的旧foreach 里面有if 吗?
  • 如果两个字段都通用?
  • @Jack 只有一个会在实际发布日期
  • @VtrioAjeesh 在这种情况下,Baba 的解决方案不会为该场景提供正确的结果。仅供参考。

标签: php arrays


【解决方案1】:

你可以使用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));

输出

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

【讨论】:

    【解决方案2】:

    你试试数组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);
    ?>
    

    上面的例子会输出:

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

    了解更多here

    文档说:

    如果要将第二个数组中的数组元素附加到 第一个数组,而不覆盖第一个数组中的元素 而不是重新索引,使用 + 数组联合运算符:

    <?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);
    ?>
    

    第一个数组中的键将被保留。如果一个数组键 存在于两个数组中,那么第一个数组中的元素将是 使用并且第二个数组中匹配键的元素将是 忽略。

    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"
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-19
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多