如何重新设置数字数组键
我有一个数组 $matrix_part
,包含数组,我想重新设置内部键以从 1 开始。
我尝试在下面编写代码,但它不起作用 - 它只是以相同的方式存储新数组。
$temp_matrix = array();
foreach ($matrix_part as $k => $v){
$temp_matrix[$k++] = $v;
}
$matrix_part = $temp_matrix;
源数组:
Array
(
[0] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
[1] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
[2] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
)
所需输出:
Array
(
[0] => Array
(
[1] => 163
[2] => 23
[3] => 97
)
[1] => Array
(
[1] => 163
[2] => 23
[3] => 97
)
[2] => Array
(
[1] => 163
[1] => 23
[3] => 97
)
)
I have an array $matrix_part
, containing arrays, and I want to rekey the inner keys to start at 1.
I am trying to code below but it doesn't work - it just stores the new array identically.
$temp_matrix = array();
foreach ($matrix_part as $k => $v){
$temp_matrix[$k++] = $v;
}
$matrix_part = $temp_matrix;
Source array:
Array
(
[0] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
[1] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
[2] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
)
Desired output:
Array
(
[0] => Array
(
[1] => 163
[2] => 23
[3] => 97
)
[1] => Array
(
[1] => 163
[2] => 23
[3] => 97
)
[2] => Array
(
[1] => 163
[1] => 23
[3] => 97
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用:
而不是这个:
这样做:
Try use:
instead of this:
do this:
这也许?
This maybe?
其中 $a 是您的输入数组
where $a is your input array
可以做类似的事情...
Could do something like ...