使用 array_map 来测试值?

发布于 2024-08-15 01:00:06 字数 780 浏览 6 评论 0原文

是否可以使用 array_map() 来测试数组的值?我想确保数组的所有元素都是数字。

我已经尝试过这两种方法

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

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

并且对于这两种方法我都得到了

TRUE
TRUE

这可以完成吗?如果是这样,我做错了什么?

注意:我知道我可以从 foreach 循环中获得我想要的功能。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小兔几 2024-08-22 01:00:06

array_map 返回一个数组。所以它总是被认为是“真实的”。现在,如果您array_search为FALSE,您也许能够获得想要的效果。

来自 PHP.net 页面

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

这意味着当前您有一个每个元素包含 true 或 false 的数组。您需要使用 array_search(false,$array) 来查找是否存在任何 false 值。

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.

初雪 2024-08-22 01:00:06

我通常是 array_map()array_filter() 等的大力倡导者,但在这种情况下 foreach() 将是最好的选择。原因是对于 array_map() 和其他方法,无论如何它都会遍历整个数组。但出于您的目的,您只需要遍历数组,直到遇到 is_numeric() 返回 false 的值,据我所知,PHP 中无法突破这些方法。

换句话说,如果数组中有 1,000 个项目,并且第 5 个不是数字,则使用 array_map() 仍会检查剩余的 995 个值,即使您已经知道该数组未通过你的测试。但如果您使用 foreach() 并在 is_numeric() == falsebreak,那么您只需要检查前五个元素。

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.

情丝乱 2024-08-22 01:00:06

您可以使用过滤器,但最终会得到一些可怕的代码

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

使用自定义函数会使它好一点,但仍然不完美

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

但是如果您使用自定义函数 array_reduce 可以工作,但它仍然有一些缺点。

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

缺点是当它找到它想要的东西时它不会中断,所以上面的功能建议不是很有效。您需要编写一个像这样的函数:

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

并像这样使用它

$isAllNumeric = array_find($arr, function($x){return !is_numeric($x);})) !== false;

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;
谷夏 2024-08-22 01:00:06

我的“标准库”中有两个微小但非常有用的函数

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

在你的例子中,

 foreach ( $arrays as $arr )
    echo all($arr, 'is_numeric') ? "ok" : "not ok";

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";
谜兔 2024-08-22 01:00:06

一种更优雅的方法恕我直言

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

 else
 {
  echo "FALSE\n";
 }
}

如果所有值都是数字,则返回 true;如果任何值不是数字,则返回 false。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文