douqi5079 2009-12-16 15:58
浏览 62
已采纳

使用array_map测试值?

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
";
        } else {
                echo "TRUE
";
        }
}

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
";
        } else {
                echo "TRUE
";
        }
}

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条回答 默认 最新

  • doutan3192 2009-12-16 16:00
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥15 关于超局变量获取查询的问题
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata