I have
$Array1 = [3=>'dog', 7=>'cat', 9=>'duck', 10=>'dog', 11=>'dog'];
$Array2 = ['cat'=>'sofa', 'dog'=>'kennel', 'duck'=>'pond'];
I want the output $ArrayOut where
我想输出$ ArrayOut在哪里
$ArrayOut = [3=>'kennel', 7=>'sofa', 9=>'pond', 10=>'kennel', 11=>'kennel'];
I can do this easily through a foreach
loop
我可以通过foreach循环轻松完成此操作
foreach ($Array1 as $key => $value) {
$ArrayOut[$key] = $Array2[$value];
}
but I was thinking if I could avoid a loop and use a combination of inbuilt PHP array functions to create the same output.
但我在想是否可以避免循环并使用内置PHP数组函数的组合来创建相同的输出。
Any clue would be helpful thanks.
任何线索都会有所帮助,谢谢。
3 个解决方案
#1
4
I think this does the trick.
我认为这样做了。
$Array1 = [3=>'dog', 7=>'cat', 9=>'duck', 10=>'dog', 11=>'dog'];
$Array2 = ['cat'=>'sofa', 'dog'=>'kennel', 'duck'=>'pond'];
$ArrayOut = array_map(function($v) use($Array2){
return $Array2[$v];
}, $Array1);
print_r($ArrayOut);
//Array ( [3] => kennel [7] => sofa [9] => pond [10] => kennel [11] => kennel )
#2
0
Try this:
$Array1 = [3=>'dog', 7=>'cat', 9=>'duck'];
$Array2 = ['cat'=>'sofa', 'dog'=>'kennel', 'duck'=>'pond'];
$new = array_flip($Array1);
$new2 = array_replace($Array2, $new);
$n = array_combine(array_merge($new2, $new), $Array2);
print_r($n);
Ouptut:
Array
(
[7] => sofa
[3] => kennel
[9] => pond
)
#3
-1
Edit: I have missread the purpose. Your best bet would be to work a foreach array in my opinion.
编辑:我错过了目的。在我看来,你最好的选择是使用foreach数组。
I'll still leave my answer here for future reference, if someone requires matching values from one array to keys of another array
如果有人要求将一个数组中的值与另一个数组的键匹配,我仍会留下我的答案以供将来参考
There is something you can use, as long as your arrays are in the order you want.
只要您的阵列符合您想要的顺序,您就可以使用它。
http://php.net/manual/en/function.array-combine.php
array_keys will extract into an array, the keys of a specified array
array_keys将提取到一个数组中,即指定数组的键
array_values will extract into an array, the values of specified array
array_values将提取到一个数组中,指定数组的值
array_combine will, as the function says, combine into an array the match arguments - being the first array argument the keys and the second the values.
正如函数所说,array_combine将把匹配参数组合成一个数组 - 第一个数组参数是键,第二个数组是值。
#1
4
I think this does the trick.
我认为这样做了。
$Array1 = [3=>'dog', 7=>'cat', 9=>'duck', 10=>'dog', 11=>'dog'];
$Array2 = ['cat'=>'sofa', 'dog'=>'kennel', 'duck'=>'pond'];
$ArrayOut = array_map(function($v) use($Array2){
return $Array2[$v];
}, $Array1);
print_r($ArrayOut);
//Array ( [3] => kennel [7] => sofa [9] => pond [10] => kennel [11] => kennel )
#2
0
Try this:
$Array1 = [3=>'dog', 7=>'cat', 9=>'duck'];
$Array2 = ['cat'=>'sofa', 'dog'=>'kennel', 'duck'=>'pond'];
$new = array_flip($Array1);
$new2 = array_replace($Array2, $new);
$n = array_combine(array_merge($new2, $new), $Array2);
print_r($n);
Ouptut:
Array
(
[7] => sofa
[3] => kennel
[9] => pond
)
#3
-1
Edit: I have missread the purpose. Your best bet would be to work a foreach array in my opinion.
编辑:我错过了目的。在我看来,你最好的选择是使用foreach数组。
I'll still leave my answer here for future reference, if someone requires matching values from one array to keys of another array
如果有人要求将一个数组中的值与另一个数组的键匹配,我仍会留下我的答案以供将来参考
There is something you can use, as long as your arrays are in the order you want.
只要您的阵列符合您想要的顺序,您就可以使用它。
http://php.net/manual/en/function.array-combine.php
array_keys will extract into an array, the keys of a specified array
array_keys将提取到一个数组中,即指定数组的键
array_values will extract into an array, the values of specified array
array_values将提取到一个数组中,指定数组的值
array_combine will, as the function says, combine into an array the match arguments - being the first array argument the keys and the second the values.
正如函数所说,array_combine将把匹配参数组合成一个数组 - 第一个数组参数是键,第二个数组是值。