Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
)

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
)

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).

like image 592
diracdeltafunk Avatar asked Jun 14 '12 23:06

diracdeltafunk


1 Answers

Using a foreach loop would be appropriate in this case:

foreach ($arr as $key => $value) {
    if (!is_int($key)) {
        unset($arr[$key]);
    }
}
like image 69
Tim Cooper Avatar answered Oct 14 '22 18:10

Tim Cooper