2

Considering array like:

$arr = array(
  'key' => 'foo'
  'key2' => 'bar',
   0 => 'num_foo',
   1 => 'num_bar'

);

How to extract values under 0 & 1? (Besides using for loop). Maybe any standard function do the job?

9
  • foreach loop is_int, is_numeric ... Commented Dec 13, 2015 at 22:00
  • Yeah, so beat me :) I just prefer inliner if I can instead of loop Commented Dec 13, 2015 at 22:00
  • Why can't you do it with a loop? Is this a school assignment? Commented Dec 13, 2015 at 22:02
  • 2
    any function, internal or user written, that iterates through an array will use a "loop". Do you even know why you don't want a loop? Commented Dec 13, 2015 at 22:02
  • 1
    $out = array_filter($arr, function ($key) { return is_numeric($key); }, ARRAY_FILTER_USE_KEY); Commented Dec 13, 2015 at 22:06

3 Answers 3

5

If you’re using a PHP version >= 5.6, you can use array_filter, with the flag that tells it to pass the key to the callback function.

$arr = array(
  'key' => 'foo',
  'key2' => 'bar',
   0 => 'num_foo',
   1 => 'num_bar'
);

$new_array = array_filter($arr, function($key) {
  return is_numeric($key);
}, ARRAY_FILTER_USE_KEY);

var_dump($new_array);

https://3v4l.org/qfar9


Edit: As Robbie pointed out, using is_numeric is the better choice here. (Previously my example used is_int.)


Edit 2: Perhaps is_int is actually the better choice here. is_numeric would return true for values such as +0123.45e6, or hexadecimal/binary number notations as well – not sure if that’s desired.

is_int would normally return false for a value such as '1'. It works here, because we are dealing with arrays – and PHP automatically converts array keys to integer, if they are an “integer string.”

Sign up to request clarification or add additional context in comments.

3 Comments

@RobbieAverill: Yeah, you’re right. I’ll edit that in, thanks.
@Robbie: Maybe is_int is the better choice here after all, see my edit.
and w/ php >= v7.4 we'd do even shorter array_filter($arr, fn($k) => is_int($k), ARRAY_FILTER_USE_KEY);
1

you can use array_filter() to return the numeric keys

// $Your_array is your original array with numeric and string keys
// array_filter() returns an array of the numeric keys
$numerickeys = array_filter(array_keys($Your_array), function($k) {return is_int($k);});

// this is a simple case where the filter function is a plain
// built-in function requiring one argument, it can be passed as a string:
// Really, this is all that's needed:
$numerickeys = array_filter(array_keys($Your_array), 'is_int');

Comments

0

If you use is_int() inside a foreach loop it's easy enough. Try this:

foreach ($arr as $key => $value) {
    if (is_int($key)) {
        echo "Found integer key with value: $value\n";
    }
}

If you desperately want to avoid using a loop, try this:

array_walk($arr, function($val, $key) {
    if (is_int($key)) {
        echo "Found integer key with value: $val\n";
    }
});

That does, of course, use a loop internally anyway.

6 Comments

Question says "besides using a loop"
His comment was "I just prefer inliner", which is a bizarre thing to say. Nevertheless I'll update my answer.
But internally we have loop written in C (am I right?) and I have big arrays.
That's a remarkable leap in logic, and definitely premature optimisation. Using array_walk() will call a PHP function once for every item in the array. Is that more efficient than looping directly in PHP? I would be surprised if so. Unless your arrays are genuinely epic in scale, give it a try with a loop first and only optimise when you need to.
Good point. Second reason for using inliner is readability improvement. I thought that there is a better way for this than array_filter etc, because in this case code readability is even worse IMO.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.