如何在2D数组中使用array_push?

时间:2014-04-28 09:09:17

标签: php arrays

我有一个像这样的数组

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 6
            [5] => 7
            [6] => 8
            [7] => 9
        )

    [4] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 4
            [3] => 5
            [4] => 6
            [5] => 7
            [6] => 8
            [7] => 9
            [8] => 10
            [9] => 11
        )

)

现在我想使用array_push关键字将其放入另一个数组中... 我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

<?php
    foreach($yourArray as $array) {
        array_push($firstArray, $array);
    }
?>

<?php
    foreach($yourArray as $array) {
       $firstArray[] = $array;
    }
?>

<?php
    array_push($firstArray, $array);
?>

答案 1 :(得分:0)

 $shiftedarray=array();
    $aftershift=array();
    foreach($twodarray as $key=>$val)
    {
        //Remove 0th index in array
        $shiftedarray[]=array_shift($val);
        //Array After Removed 0th index
        $aftershift[]=$val;

    }
    echo "<pre>";
    print_r($shiftedarray);
    print_r($aftershift);
   $oneDimensionalArray = call_user_func_array('array_merge', $aftershift);

print_r($oneDimensionalArray);