使用另一个数组中的值更新所有数组键 [英] Update all array keys using value from another array
本文介绍了使用另一个数组中的值更新所有数组键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用存储在 $ aKeyNames
中的文本等效项替换 $ aValues
中的所有数字键.
I would like to replace all numerical keys in $aValues
with a textual equivalent stored in $aKeyNames
.
$aKeyNames = array(0 => 'foo', 1 => 'bar');
$aValues = array(0 => 'foo content', 1 => 'bar content');
所需的输出;
$aValues = array('foo' => 'foo content', 'bar' => 'bar content');
为此,我编写了以下工作代码;
To achieve this I've written the following working code;
foreach ($aValues as $iPos => $aValue) {
$aValues[ $aKeyNames[$iPos] ] = $aValue;
unset($aValues[$iPos]);
}
我担心的是 $ aValues
很大.有没有更有效的方法来实现这一目标?
My concern is that $aValues
is very large. Is there a more efficient way to achieve this?
推荐答案
请参阅 array_combine 的手册:>
Refer to manual for array_combine:
print_r(array_combine($aKeyNames, $aValues));
这篇关于使用另一个数组中的值更新所有数组键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文