phparraysassociative-array

Populate an associative array with keys defined by an array and values from a static value


Array 1:

Array (
    '127.0.0.1', 
    '235.107.12.3' 
)

Array 2:

Array (
    '34.235.54.6',
    '230.56.78.1'
)

Final Array should like below:

Array (
    [127.0.0.1] => Array (
        '34.235.54.6',
        '230.56.78.1'
    ), 
    [235.107.12.3]' => Array (
        '34.235.54.6',
        '230.56.78.1'
    ) 
)

Please give an advice as to how I can merge these two arrays (array 1 and array 2) to achieve the desired result.


Solution

  • Use array_fill_keys:

    $final = array_fill_keys( $array1, $array2 );