Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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;

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";

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top