Add new element to array of array php

In PHP, you can add a new element to an array of arrays using the following methods:

Method 1: Using the [] operator

$array = [['a', 'b'], ['c', 'd']];
$array[] = ['e', 'f']; // adds a new element to the end of the array
print_r($array);

Output:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
            [1] => f
        )

)

Method 2: Using the array_push() function

$array = [['a', 'b'], ['c', 'd']];
array_push($array, ['e', 'f']);
print_r($array);

Output:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
            [1] => f
        )

)

Method 3: Using the array_merge() function

$array = [['a', 'b'], ['c', 'd']];
$array = array_merge($array, [['e', 'f']]);
print_r($array);

Output:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
            [1] => f
        )

)

Note that in all cases, the new element is added to the end of the array. If you want to add the new element at a specific position, you can use the array_splice() function or the array_merge() function with the preserve_keys option set to true.