添加两个多维数组,并将它们的所有项目添加到另一个数组中

时间:2018-08-29 06:14:50

标签: php arrays

我有两个多维数组,想将它们与所有项目结合在一起。 可以重新索引不会有任何问题。

我已经在foreach循环中创建了这些数组,并将其用于批处理插入查询。

这是数组,我想将它们都添加

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [question_def_id] => 1
                    [description] => 3
                    [description_hindi] => 3
                    [correct] => 0
                )

            [1] => Array
                (
                    [question_def_id] => 1
                    [description] => 4
                    [description_hindi] => 4
                    [correct] => 1
                )

            [2] => Array
                (
                    [question_def_id] => 1
                    [description] => 5
                    [description_hindi] => 5
                    [correct] => 0
                )

            [3] => Array
                (
                    [question_def_id] => 1
                    [description] => 6
                    [description_hindi] => 6
                    [correct] => 0
                )

        )
)

2 个答案:

答案 0 :(得分:0)

加入两个多维数组

$test_array1 = array(array('question_def_id'=>1,'description'=>2),    array('question_def_id'=>3,'description'=>4));
$test_array2 = array(array('question_def_id'=>5,'description'=>6), array('question_def_id'=>7,'description'=>8));
 $C = array_merge($test_array1, $test_array2);
            print_r($C);

答案 1 :(得分:-1)

您可以使用PHP array_merge_recursive函数将两个数组合并为一个。

请参见here

例如

<?php
  $ar1 = array("color" => array("favorite" => "red"), 5);
  $ar2 = array(10, "color" => array("favorite" => "green", "blue"));
  $result = array_merge_recursive($ar1, $ar2);
  print_r($result);
?>