1

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.

5 Answers 5

5

Use array_fill_keys:

$final = array_fill_keys( $array1, $array2 );
Sign up to request clarification or add additional context in comments.

Comments

1

Try this one

$a = array_fill_keys($array1, $array2);
Print_r($a);

Output:

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'
        ) 
   )

Comments

0

you can get your job done using loop, for eg. foreach loop here

foreach($array1 AS $val1)
{
    foreach($array2 AS $val2)
    {
         $newarr[$val1][] = $val2;
    }

}

print_r($newarr);

Comments

0
<?php
    $final = array();
    foreach($array1 as $k => $v)
        $final[$k] = $array2;

    var_dump($final);
?>

Comments

-1
$arrayA = array('127.0.0.1','235.107.12.3' );
$arrayB = array('34.235.54.6','230.56.78.1');
$i = 0;
foreach($arrayA as $a){
   $arrayC[$i] = arrayB;
$i++;
}

1 Comment

-1. $i remains zero and arrayB should be $arrayB. Also, please refrain from code-only answers: add some descriptive text, please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.