PHP 对齐数组键值
我在 Google 上搜索了两天,并尝试查看 PHP 手册,但我仍然不记得那个对齐 PHP 数组键值的函数。
我正在寻找的是接受这个的函数:
Array
(
[0] => 1
[3] => 2
[4] => 3
[7] => 4
[9] => 5
)
并将其转换为:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
基本上,数组首先按键排序(附加到它们的值与它们保持一致),然后所有键都设置为所有计数数字,连续,不跳过任何数字(0,1,2,3,4,5,6,7,8,9...)。几个月前我看到它与 ksort() 一起使用,但无法记住或找到这个难以捉摸的函数。
I've Googled it for two days, and tried looking at the PHP manual, and I still can't remember that function that aligns the key values for PHP arrays.
All I'm looking for is the function that takes this:
Array
(
[0] => 1
[3] => 2
[4] => 3
[7] => 4
[9] => 5
)
And converts it into this:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Basically, the array is first sorted by key (their values attached to them stay with them), then all the keys are set to all the counting numbers, consecutively, without skipping any number (0,1,2,3,4,5,6,7,8,9...). I saw it being used with ksort() a few months ago, and can't see to remember or find this elusive function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,你看,这个很难,因为 PHP 数组函数页面上的一般描述并没有说这个函数可以实现你正在寻找的功能。
但是您可以使用 ksort() 对数组进行排序,然后使用 array_values() 。来自 PHP 手册的页面:
Well, you see, this one is hard, because the general description on the PHP array functions page does not say that this function does what you're looking for.
But you can sort the array using
ksort()
, and then use this:array_values()
. From the page from the PHP manual:您可以使用
array_merge
:它将用数字键重新索引值。
更新:按照@LostInTheCode 的回答可能更具描述性。
You can use
array_merge
:It will reindex values with numeric keys.
Update: Using
array_values
as proposed in @LostInTheCode's answer is probably more descriptive.然后像常规函数一样使用,应该重新索引数组,
您还可以使用本机函数,例如 array_values ,它将数组的值返回到单维数组中,从而使其被重新索引。
And then use like a regular function, should re index the array
you can also use native functions such as
array_values
which returns the values of an array into a single dimension array, causing it to be re indexed .