PHP 数组修剪空白索引值
如何修剪 PHP 数组并删除所有空索引
Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] => 4
[8] => 6
[9] =>
)
输出应该类似于
Array
(
[0] => 4
[1] => 6
)
How to trim a PHP array and remove all empty indexes
Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] => 4
[8] => 6
[9] =>
)
Output should be like
Array
(
[0] => 4
[1] => 6
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在寻找
array_filter
函数 ;-)例如,这部分代码:
将为您提供以下输出:
请注意,所有“假”值已被删除。
如果你想更具体,你可以指定你自己的过滤功能。例如,要从数组中仅删除
null
,我可以使用这个:我会得到:
You are looking for the
array_filter
function ;-)For instance, this portion of code :
Will give you the following output :
Note that all "falsy" values have been removed.
And if you want to be more specific, you can specify your own filtering function. For instance, to remove only
null
s from the array, I could use this :And I'd get :
另一种方式:
输出将是:
Here another way:
Output will be:
对我来说听起来像是家庭作业。
我建议你看一下 array_filter 函数。这似乎是最合适的选择。
Sounds like homework to me.
I'd suggest you take a look at the array_filter function. That seems to be the most appropriate option.