将两个相似的多维数组合并为一个

时间:2013-05-07 05:43:02

标签: php arrays

我有这个数组:

$arr1 = array (
    array (
        array (
            'correct' => FALSE,
            'answer' => 'false answer1'
        ),  
        array (
            'correct' => FALSE,
            'answer' => 'false answer2'
        ),  
        array (
            'correct' => FALSE,
            'answer' => 'false answer3'
        )   
    ));

这个数组:

$arr2 = array (
    array ( 
        array (
            'correct' => TRUE,
            'answer' => 'correct answer'
        )   
    ));

我希望输出为:

$output = array (
    array (
        array (
            'correct' => FALSE,
            'answer' => 'false answer1'
        ),
        array (
            'correct' => FALSE,
            'answer' => 'false answer2'
        ),
        array (
            'correct' => FALSE,
            'answer' => 'false answer3'
        ),                  
        array (
            'correct' => TRUE,
            'answer' => 'correct answer'
        )
    ));

我尝试了using array_mergearray_merge_recursive,但它合并的不是我想要的方式(它只是在底部添加了值而不是正确地将它们放入每个数组中)。我也看到了类似的问题,但它没有帮助我(仍然合并底部的值而不是我希望他们如何)。

有什么建议吗?

3 个答案:

答案 0 :(得分:3)

试试这个

$array[] = array_merge($arr1[0],$arr2[0]);
print_r($array);

Codepad

输出

Array
(
[0] => Array
    (
        [0] => Array
            (
                [correct] => 
                [answer] => false answer1
            )

        [1] => Array
            (
                [correct] => 
                [answer] => false answer2
            )

        [2] => Array
            (
                [correct] => 
                [answer] => false answer3
            )

        [3] => Array
            (
                [correct] => 1
                [answer] => correct answer
            )
    )
)

答案 1 :(得分:1)

试试这个,

array_merge($arr1[0], $arr2[0]);

答案 2 :(得分:0)

  $arr1= array (
                array (
                        array (
                                  'correct' => FALSE,
                                  'answer' => 'false answer1'
                        ),
                        array (
                                  'correct' => FALSE,
                                  'answer' => 'false answer2'
                        ),
                        array (
                                  'correct' => FALSE,
                                  'answer' => 'false answer3'
                        )
                )
                );
      $arr2= array (
                    array ( 
                        array (
                                  'correct' => TRUE,
                                  'answer' => 'correct answer'
                        )
                )
                );  

      $result=array(array_merge($arr1[0],$arr2[0]));
      print_r($result);

输出

  Array
(
    [0] => Array
    (
        [0] => Array
            (
                [correct] => 
                [answer] => false answer1
            )

        [1] => Array
            (
                [correct] => 
                [answer] => false answer2
            )

        [2] => Array
            (
                [correct] => 
                [answer] => false answer3
            )

        [3] => Array
            (
                [correct] => 1
                [answer] => correct answer
            )

    )

 )