如何组合两个数组[重复]

时间:2022-06-27 09:08:32

This question already has an answer here:

这个问题在这里已有答案:

I have two arrays

我有两个数组

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


Array ( [1] => Zwembad [2] => Airconditioning [3] => Telefoon [4] =>
Internet [5] => Wi-Fi [6] => TV [11] => food )

Is there an array function to combine this array to form a new array.

是否有一个数组函数来组合此数组以形成一个新数组。

Result Array should be:

结果数组应该是:

Array ( [0] => Zwembad  [1] => Airconditioning  [2] => Internet  [3] => Wi-Fi [4] => 6 [5] => TV )

That is, the values of First array have been replaced by the values corresponding to the index of second array.

也就是说,First数组的值已被与第二个数组的索引对应的值替换。

4 个解决方案

#1


1  

Why not have options?

为什么没有选择?

$new = array();
$i = 0;
$ak = array_values( $array2 );
foreach ( array_keys( $array1 ) as $k ) 
{
    $new[$k] = $ak[$i];
    $i++;
}

#2


0  

Try with array_values

尝试使用array_values

$second_array = array_values($second_array);

#3


0  

You can use array_combine.

您可以使用array_combine。

$combineArray=array_combine($array1, $array2);

#4


0  

<?php
$a1 = array("red","green");
$a2 =array("blue","yellow");
print_r(array_merge($a1,$a2));
?>

#1


1  

Why not have options?

为什么没有选择?

$new = array();
$i = 0;
$ak = array_values( $array2 );
foreach ( array_keys( $array1 ) as $k ) 
{
    $new[$k] = $ak[$i];
    $i++;
}

#2


0  

Try with array_values

尝试使用array_values

$second_array = array_values($second_array);

#3


0  

You can use array_combine.

您可以使用array_combine。

$combineArray=array_combine($array1, $array2);

#4


0  

<?php
$a1 = array("red","green");
$a2 =array("blue","yellow");
print_r(array_merge($a1,$a2));
?>