phparraysmultidimensional-array

How to declare 3 flat arrays as a 2d array with 3 rows?


How can I push a few arrays into a single multidimensional array?

$array1 = ['apple' => 1, 'mango' => 5, 'banana' => 3];
$array2 = ['car' => 2, 'bus' => 7, 'moto' => 8];
$array3 = ['rain' => 14, 'snow' => 7, 'sun' => 12];

Desired new array:

$newArray = [
    ['apple' => 1, 'mango' => 5, 'banana' => 3],
    ['car' => 2, 'bus' => 7, 'moto' => 8],
    ['rain' => 14, 'snow' => 7, 'sun' => 12]
];

Solution

  • It's really very simple

    $newArray = array($array1, $array2, $array3);
    

    or more literally

    $newArray = array(
        array('apple' => 1, 'mango' => 5, 'banana' => 3),
        array('car' => 2, 'bus' => 7, 'moto' => 8),
        array('rain' => 14, 'snow' => 7, 'sun' => 12)
    );