所以考虑一个包含我最喜欢的3种水果的数组:
$array1 = array("Apple", "Banana","Raspberry")
我想将它与自己美丽而自然的颜色合并
$array2 = array("Green ", "Yellow ","Red ")
以便结果看起来像
([0] => Green Apple [1] => Yellow Banane [2] => Red Raspberry)
我需要一些可扩展的东西(2到6个键,在数组之间总是相同)
我尝试了什么和结果
array_combine($array2,$array1)
结果:Array ( [Green ] => Apple
[Yellow ] => Banana [Red ] => Raspberry )
array_merge($array2,$array1)
结果:Array ( [0] => Green [1] =>
Yellow [2] => Red [3] => Apple [4] => Banana [5] => Raspberry )
array_merge_recursive($array2,$array1)
结果:Array ( [0] =>
Green [1] => Yellow [2] => Red [3] => Apple [4] => Banana [5] =>
Raspberry )
答案 0 :(得分:2)
你实际应该循环遍历数组来组合它们。
$combinedArray = array();
foreach ( $array1 as $key=>$value ) {
$combinedArray[$key] = $array2[$key] . ' ' . $array1[$key];
}
答案 1 :(得分:1)
为什么不简单地遍历每个数组。
$array1 = array("Apple", "Banana","Raspberry");
$array2 = array("Green ", "Yellow ","Red ")
$array3 = arrayCombine($array1, $array2);
function arrayCombine($array1, $array2) {
$array_out = array();
foreach ($array1 as $key => $value)
$array_out[] = $value . ' ' . $array2[$key];
return $array_out;
}