嗨我有两个阵列,第一个看起来像这样:
array(
00000 => array(
0 => 123,
1 => 456,
2 => 789,
)
11111 => array(
0 => 987,
1 => 654,
2 => 321,
)
我的第二个数组看起来像这样:
array(
00000 => 'apples',
11111 => 'bananas',
)
我要做的是将第一个数组中的键与第二个数组中的键匹配,然后如果它们匹配,则获取第一个数组的键对值数组的值,并使它们成为全新数组的键和值第二个数组使它们成为全新数组的值。像这样:
array(
123 => 'apples',
456 => 'apples',
789 => 'apples',
987 => 'bananas',
654 => 'bananas',
321 => 'bananas',
)
任何帮助谢谢!
答案 0 :(得分:1)
所以我假设您有2个数组(并且您修复了第二个数组以获得唯一键)
$array3 = array (); //for the result
foreach ($array1 as $seg)
{
foreach ($seg as $key)
{
$array3[$key] = $array2[$seg];
}
}
答案 1 :(得分:0)
试试这个
$array = array(
"00000" => array(
0 => 123,
1 => 456,
2 => 789,
),
"11111" => array(
0 => 987,
1 => 654,
2 => 321,
)
);
$arr = array(
"00000" => 'apples',
"11111" => 'bananas',
);
$array3 = array();
foreach ($array as $keyas => $segs)
{
foreach ($segs as $key)
{
$array3[$key] = $arr[$keyas];
}
}
echo "<pre>";
print_r($array3);
unset($array3);
输出:
Array
(
[123] => apples
[456] => apples
[789] => apples
[987] => bananas
[654] => bananas
[321] => bananas
)
答案 2 :(得分:0)
$array3 = array_keys($array1);
$array4 = array_keys($array2);
$array5 = array_intersect($array3, $array4)
$array6 = array();
foreach($array5 as $id) {
foreach($array1[$id] as $userId) {
$array6[$userId] = $array2[$id]
}
}
可能会生成比所需更多的数组,但这会在为新数组赋值之前匹配两个数组的键。
感谢所有回答和帮助的人!