dpkrbe395930 2014-05-13 19:03
浏览 58
已采纳

仅打印多维数组中包含特定值的行

I have this array in PHP 5.2.3 :

$a = array (
    array("a","c","1");
    array("b","a","2");
    array("a","b","3");
    array("b","b","4");
    array("a","a","5");
);

and I would like to "select" (or create a new array) only the rows that have the first element "a". Like this :

"a","c","1"
"a","b","3"
"a","a","5"

How can I do this ?

EDIT :

<?php
    $a = array (
        array("a","c","1"),
        array("b","a","2"),
        array("a","b","3"),
        array("b","b","4"),
        array("a","a","5"),
    );

    $result = array_filter(
        $array,
        'testFirst'
    );

    print_r($result);
?>

I get this error Warning: array_filter() [function.array-filter]: The first argument should be an array in C:\wamp\www\keySearch\test.php on line 13

FINAL EDIT :

<?php
    $a = array (
        array("a","c","1"),
        array("b","a","2"),
        array("a","b","3"),
        array("b","b","4"),
        array("a","a","5"),
    );

    function testFirst($value) {
        return($value[0] == 'a');
    }

    $result = array_filter($a, testFirst);
    print_r($result);
?>
  • 写回答

2条回答 默认 最新

  • dtotwai720621 2014-05-13 19:07
    关注
    function testFirst($value) {
        return($value[0] == 'a');
    }
    
    $result = array_filter(
        $a,
        'testFirst'
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置