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

我希望输出为:

$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_merge, array_merge_recursive,但它并没有按照我想要的方式合并它(它只是在底部添加了值,而不是正确地将它们放入每个数组中)。我也看到了与此类似的问题,但它对我没有帮助(仍然合并底部的值,而不是我想要的方式)。

有什么建议么?

4

3 回答 3

3

尝试这个

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

键盘

输出

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
            )
    )
)
于 2013-05-07T05:48:04.370 回答
1

尝试这个,

array_merge($arr1[0], $arr2[0]);
于 2013-05-07T05:47:03.473 回答
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
            )

    )

 )
于 2013-05-07T05:49:27.770 回答