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 找人不需要人工智能回答的gamit解算后做形变分析
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错