array_combine 删除了重复的数组
array_combine removed duplicate array
Array_combine 删除重复数组
<?php
$a1=array("red","red");
$a2=array("blue","yellow");
print_r(array_combine($a1,$a2));
?>
此代码给出输出:Array ( [red] => yellow )
但我想要这样的输出:Array ( [red] => blue [red] => yellow )
安德烈亚斯的回答是正确的。你可以这样做:
$a1 = ['red'];
$a2 = ['blue', 'yellow'];
$a3 = [];
foreach($a1 as $item1) {
foreach($a2 as $item2) {
$a3[$item1][] = $item2;
}
}
print_r($a3);
输出:
array(1) {
["red"]=>
array(2) {
[0]=>
string(4) "blue"
[1]=>
string(6) "yellow"
}
}
Array_combine 删除重复数组
<?php
$a1=array("red","red");
$a2=array("blue","yellow");
print_r(array_combine($a1,$a2));
?>
此代码给出输出:Array ( [red] => yellow )
但我想要这样的输出:Array ( [red] => blue [red] => yellow )
安德烈亚斯的回答是正确的。你可以这样做:
$a1 = ['red'];
$a2 = ['blue', 'yellow'];
$a3 = [];
foreach($a1 as $item1) {
foreach($a2 as $item2) {
$a3[$item1][] = $item2;
}
}
print_r($a3);
输出:
array(1) {
["red"]=>
array(2) {
[0]=>
string(4) "blue"
[1]=>
string(6) "yellow"
}
}