1

Is it possible to use array_map() to test values of an array? I want to make sure that all elements of an array are numeric.

I've tried both

$arrays = array(
         array(0,1,2,3 )
        , array ( 0,1, "a", 5 )
);

foreach ( $arrays as $arr ) {

        if ( array_map("is_numeric", $arr) === FALSE ) {
                echo "FALSE\n";
        } else {
                echo "TRUE\n";
        }
}

and

$arrays = array(
         array(0,1,2,3 )
        , array ( 0,1, "a", 5 )
);

foreach ( $arrays as $arr ) {

        if ( ( array_map("is_numeric", $arr) ) === FALSE ) {
                echo "FALSE\n";
        } else {
                echo "TRUE\n";
        }
}

And for both I get

TRUE
TRUE

Can this be done? If so, what am I doing wrong?

Note: I am aware that I can get my desired functionality from a foreach loop.

5 Answers 5

4

array_map returns an array. So it will always be considered 'true'. Now, if you array_search for FALSE, you might be able to get the desire effects.

From the PHP.net Page

array_map() returns an array containing all the elements of 
arr1 after applying the callback function to each one.

This means that currently you have an array that contains true or false for each element. You would need to use array_search(false,$array) to find out if there are any false values.

2

I'm usually a big advocate of array_map(), array_filter(), etc., but in this case foreach() is going to be the best choice. The reason is that with array_map() and others it will go through the entire array no matter what. But for your purposes you only need to go through the array until you run into a value for which is_numeric() returns false, and as far as I know there's no way in PHP to break out of those methods.

In other words, if you have 1,000 items in your array and the 5th one isn't numeric, using array_map() will still check the remaining 995 values even though you already know the array doesn't pass your test. But if you use a foreach() instead and have it break on is_numeric() == false, then you'll only need to check those first five elements.

1
  • This is good advice, But Chacha says that array_map always returns an array, so the answer to the question "No, you can't use it how you want because it won't give you a single FALSE value if it encounters a non-numeric element". So you'd have to do foreach() anyway, in which case using break is very good advice :)
    – user151841
    Dec 16, 2009 at 16:30
1

You could use filter, but it ends up with a horrible bit of code

$isAllNumeric = count(array_filter($arr, "is_numeric")) === count($arr)

Using a custom function makes it a bit better, but still not perfect

$isAllNumeric = count(array_filter($arr, function($x){return !is_numeric($x);})) === 0

But if you were using custom functions array_reduce would work, but it still has some failings.

$isAllNumeric = array_reduce($arr,
                             function($x, $y){ return $x && is_numeric($y); },
                             true);

The failings are that it won't break when it has found what it wants, so the functional suggestions above are not very efficient. You would need to write a function like this:

function array_find(array $array, $callback){
    foreach ($array as $x){ //using iteration as PHP fails at recursion
        if ( call_user_func($callback, array($x)) ){
            return $x;
        }
    }
    return false;
}

And use it like so

$isAllNumeric = array_find($arr, function($x){return !is_numeric($x);})) !== false;
1
  • A nice summing up of the possibilities, and an even nicer extension to PHP!
    – xtofl
    Dec 16, 2009 at 18:11
1

i have two tiny but extremely useful functions in my "standard library"

function any($ary, $func) {
   foreach($ary as $val)
      if(call_user_func($func, $val)) return true;
   return false;
}

function all($ary, $func) {
   foreach($ary as $val)
      if(!call_user_func($func, $val)) return false;
   return true;
}

in your example

 foreach ( $arrays as $arr )
    echo all($arr, 'is_numeric') ? "ok" : "not ok";
2
  • Those look handy, stereo! Pursuant to Jordan's comments, would you see a value in adding a break statement to the any() function?
    – user151841
    Dec 16, 2009 at 17:09
  • 1
    In this case no, since return stops the execution much like break does.
    – Alix Axel
    Dec 16, 2009 at 17:54
0

A more elegant approach IMHO:

foreach ($arrays as $array)
{
 if (array_product(array_map('is_numeric', $array)) == true)
 {
  echo "TRUE\n";
 }

 else
 {
  echo "FALSE\n";
 }
}

This will return true if all the values are numeric and false if any of the values is not numeric.

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.