根据另一个数组的值设置数组的键
$array1 = array( [0] => 2 [1] => 3 [2] => 5 [3] => 6 [4] => 7 [5] => 8 [6] => 9 ) ;
$array2 = array( ( [0] => Array (...) [1] => Array (...) [2] => Array (...) ... [6] => Array (...));
它们具有相同数量的元素。我希望 array2 将 array1 的值作为键。
应该变成:
$array2 = array( ( [2] => Array (...) [3] => Array (...) [5] => Array (...) ... [9] => Array (...));
我已经编写了这段代码,但是,我不知道为什么它只返回 array2 的一部分。
reset($array1);reset($array2);
foreach ($array2 as $k => $v) {
unset ($array2[$k]);
$new_key = current($array1);
next($array1);
$array2[$new_key] = $v;
}
print_r($array2);
提前致谢。
$array1 = array( [0] => 2 [1] => 3 [2] => 5 [3] => 6 [4] => 7 [5] => 8 [6] => 9 ) ;
$array2 = array( ( [0] => Array (...) [1] => Array (...) [2] => Array (...) ... [6] => Array (...));
They have the same number of elements. I want the array2 to have, as keys, the values of array1.
Should become:
$array2 = array( ( [2] => Array (...) [3] => Array (...) [5] => Array (...) ... [9] => Array (...));
I have written this code but, I dunno why it returns only a part of array2.
reset($array1);reset($array2);
foreach ($array2 as $k => $v) {
unset ($array2[$k]);
$new_key = current($array1);
next($array1);
$array2[$new_key] = $v;
}
print_r($array2);
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 array_combine 创建一个对象,其中键来自一个数组,值来自另一个数组。
输出文档
PHP: array_combine 的
Use
array_combine
to create an object with keys from one array, and the values from another.output
Documentation of PHP: array_combine