用非数字键删除数组的所有元素 [英] Remove all elements of an array with non-numeric keys
本文介绍了用非数字键删除数组的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的数组:
I have an array that looks something like this:
Array
(
[0] => apple
["b"] => banana
[3] => cow
["wrench"] => duck
)
我想使用该数组并使用array_filter或类似的方法来删除具有非数字键的元素并接收以下数组:
I want to take that array and use array_filter or something similar to remove elements with non-numeric keys and receive the follwoing array:
Array
(
[0] => apple
[3] => cow
)
我正在考虑这个问题,我想不出一种解决方法,因为array_filter不能为我的函数提供键,而array_walk无法修改数组结构(在PHP手册中已提及).
I was thinking about this, and I could not think of a way to do this because array_filter does not provide my function with the key, and array_walk cannot modify array structure (talked about in the PHP manual).
推荐答案
在这种情况下,使用 foreach
循环将是合适的:
Using a foreach
loop would be appropriate in this case:
foreach ($arr as $key => $value) {
if (!is_int($key)) {
unset($arr[$key]);
}
}
这篇关于用非数字键删除数组的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文