PHP merge two-dimensional arrays

Question

Answers ( 1 )

    0
    2024-01-11T18:05:26+00:00

    Method 1: array_merge()

    If you want to merge the arrays such that the second array's elements are appended to the first, you can use array_merge(). This method does not preserve numeric keys.

    $array1 = [[1, 2], [3, 4]];
    $array2 = [[5, 6], [7, 8]];
    
    $result = array_merge($array1, $array2);
    print_r($result);
    

    This will output:

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

    Method 2: array_merge_recursive()

    If you want to merge arrays and combine values with the same keys, you can use array_merge_recursive().

    $array1 = ['a' => [1, 2], 'b' => [3, 4]];
    $array2 = ['a' => [5, 6], 'b' => [7, 8]];
    
    $result = array_merge_recursive($array1, $array2);
    print_r($result);
    

    This will output:

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

    Method 3: array_combine()

    In case you want to merge two arrays into a key-value pair, array_combine() can be used. This method assumes that one array will be the keys and the other will be the values.

    $keys = ['a', 'b'];
    $values = [[1, 2], [3, 4]];
    
    $result = array_combine($keys, $values);
    print_r($result);
    

    This will output:

    Array
    (
        [a] => Array
            (
                [0] => 1
                [1] => 2
            )
    
        [b] => Array
            (
                [0] => 3
                [1] => 4
            )
    )
    

    Method 4: Manual Looping

    You can also manually loop through the arrays and merge them as per your specific requirements.

    $array1 = [[1, 2], [3, 4]];
    $array2 = [[5, 6], [7, 8]];
    
    $result = [];
    
    foreach ($array1 as $key => $value) {
        $result[$key] = array_merge($value, $array2[$key]);
    }
    
    print_r($result);
    

    This will output:

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

    Each method serves a different purpose, so the best choice depends on the specific requirements of your task.

Leave an answer