Search code examples
phparraysarray-merge

Multiple arrays merging issue


I have two arrays

First array

array(
    [0] => +970
    [1] => +971
    [2] => +972
)

And Second array

array(
    [0] => 465465454
    [1] => 321321355
    [2] => 987946546
)

I want to merge them like this

array(
    [+970] => 465465454
    [+971] => 321321355
    [+972] => 987946546
)

I try with array_merge but this gives me the result that I didn't want e.g.

$busi_code  = $page1_data->business_code; //array
$busi_num  = $page1_data->business_number; //array

$business_phone_numbers = array_merge($busi_code, $busi_num);

echo '<pre>';
print_r($business_phone_numbers);
echo '</pre>';

And its result is

[0] => +970
[1] => +971
[2] => +972
[3] => 465465454
[4] => 321321355
[5] => 987946546

So please guide me how can I achieve my required result.


Solution

  • You're looking for array_combine, rather than array_merge:

    Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.

    $business_phone_numbers = array_combine($busi_code, $busi_num);
    

    See https://eval.in/954799