PHP - 将正常结构化的数组附加到多维数组中的嵌套数组

时间:2018-05-08 22:59:02

标签: php arrays multidimensional-array

我有两个数组。一个是多维数组,另一个是正常结构,如下所示。

 Array ( 
           [0] = Array
              (
                 [0] => Array 
                        (
                         [james] => 1
                         [kevin] => 2
                        )
                 [1] => Array
                        (
                         [joe] => 1
                         [jim] => 2
                        )
               )
           [1] = Array
              (
                 [0] => Array 
                        (
                         [jill] => 1
                         [john] => 2
                        )
                 [1] => Array
                        (
                         [janet] => 1
                         [clarence] => 2
                        )
               )
      )

和第二个数组

Array
(
    [0] => Array
        (
            [total_stuff] => 75210

        )

    [1] => Array
        (
            [total_stuff] => 95640

        )

)

如何将第二个数组的第一个值附加到多维数组中第一个内部数组的末尾,以便它看起来像下面的数组?我需要保留第二个数组的值,而不是键。

 Array ( 
               [0] = Array
                  (
                     [0] => Array 
                            (
                             [james] => 1
                             [kevin] => 2
                            )
                     [1] => Array
                            (
                             [joe] => 1
                             [jim] => 2
                            )
                     [2] => Array
                            (
                            [total_stuff] => 75210
                            )
                   )
               [1] = Array
                  (
                     [0] => Array 
                            (
                             [jill] => 1
                             [john] => 2
                            )
                     [1] => Array
                            (
                             [janet] => 1
                             [clarence] => 2
                            )
                     [2] => Array
                            (
                            [total_stuff] => 95640
                            )
                   )
          )

1 个答案:

答案 0 :(得分:0)

您可以使用数组映射和数组合并

来完成
<?php
$a1=array ( 
           array
              (
                  array 
                        (
                         "james" => 1,
                         "kevin" => 2
                        ),
                 array
                        (
                         "joe" => 1,
                         "jim" => 2
                        )
               ),
           array
              (
                  array 
                        (
                         "jill" => 1,
                         "john" => 2
                        ),
                   array
                        (
                         "janet" => 1,
                         "clarence" => 2
                        )
               )
      );

$a2=array
(
     array
        (
            "total_stuff" => 75210

        ),

    array
        (
            "total_stuff" => 95640

        )

);
//merge each index with corresponding index of second array to form new array as you desired
$new = array_map(function ($a,$k)use($a2) { return array_merge($a,array($a2[$k])); }, $a1,array_keys($a1));
echo "<pre>";
print_r($new);
?>

输出

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [james] => 1
                    [kevin] => 2
                )

            [1] => Array
                (
                    [joe] => 1
                    [jim] => 2
                )

            [2] => Array
                (
                    [total_stuff] => 75210
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [jill] => 1
                    [john] => 2
                )

            [1] => Array
                (
                    [janet] => 1
                    [clarence] => 2
                )

            [2] => Array
                (
                    [total_stuff] => 95640
                )

        )

)

工作小提琴http://phpfiddle.org/main/code/ymf7-69si