19

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

3
  • 5
    Does it matter if the key is a string representation of the number? i.e. "0"
    – hafichuk
    Commented Jun 14, 2012 at 23:06
  • Preferably, i would like to exclude that case, just be cause it seems more correct, but it does not matter in my current application. Commented Jun 14, 2012 at 23:07
  • 2
    doing more work than necessary is never "more correct"
    – user557846
    Commented Jun 14, 2012 at 23:09

4 Answers 4

38

Using a foreach loop would be appropriate in this case:

foreach ($arr as $key => $value) {
    if (!is_int($key)) {
        unset($arr[$key]);
    }
}
1
  • PHP newbie here. I had a 2D array, and I used this to update my inner loop. I could tell from print statements that the inner loop was being updated, but for some reason the entire 2D array stayed the same. I had to add another line at the end. So if the outer loop was foreach ($everything as $index => $arr), you need $everything[$index] = $arr;.
    – Bartleby
    Commented Apr 25, 2020 at 6:21
25

It can be done without writing a loop in one (long) line:

$a = array_intersect_key($a, array_flip(array_filter(array_keys($a), 'is_numeric')));

What it does:

  • Since array_filter works with values, array_keys first creates a new array with the keys as values (ignoring the original values).
  • These are then filtered by the is_numeric function.
  • The result is then flipped back so the keys are keys once again.
  • Finally, array_intersect_key only takes the items from the original array having a key in the result of the above (the numeric keys).

Don't ask me about performance though.

4
  • How can i check the array key not numeric. I am trying with is_string function in array_filter callback but getting an error
    – karim_fci
    Commented Aug 29, 2015 at 10:13
  • What error are you getting? As you can see here, this seems to work: 3v4l.org/aj9FD
    – smhg
    Commented Aug 29, 2015 at 13:24
  • 2
    for sake of truth, array_filter actually is a loop. You are not "writing" a loop, but internally it is.
    – kekko12
    Commented Mar 10, 2016 at 10:54
  • If you choose to use this answer, keep in mind that despite of array_intersect_key with is_numeric filter, you can simply use is_string filter.
    – JoDev
    Commented Feb 19, 2018 at 9:43
25

As of PHP 5.6, it's now possible to use array_filter in a compact form:

array_filter($array, function ($k) { return is_numeric($k); }, ARRAY_FILTER_USE_KEY);

Demo.

This approach is about 20% slower than a for loop on my box (1.61s vs. 1.31s for 1M iterations).


As of PHP 7.4, it's possible to also use short closures::

array_filter($array, fn($k) => is_numeric($k), ARRAY_FILTER_USE_KEY);
1
  • 16
    You can also just pass the function name as a string. I.e. array_filter($array, "is_numeric", ARRAY_FILTER_USE_KEY);
    – Sean
    Commented Oct 19, 2016 at 23:56
7

Here's a loop:

foreach($arr as $key => $value) {
    if($key !== 0 and !intval($key)) {
         unset($arr[$key]);
    }
}
1
  • 10
    Because !intval(0) is true. Commented Jun 14, 2012 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.