dongrong9938 2014-01-08 07:46
浏览 123
已采纳

使用正则表达式在数组中查找某个值的最短代码?

Lets say I have an array with these elements:

[
    'whatever',
    'something else',
    'foobar'
]

I know I can iterate over the array and check for foobar using a regex for example. The question is, is there a shorter way of doing that?

Thanks?

  • 写回答

2条回答 默认 最新

  • dsrbb20862 2014-01-08 07:51
    关注
    <?php
    
    $items = [
        'whatever',
        'something else',
        'foobar'
    ];
    
    $matches  = preg_grep ('/foobar/', $items);
    var_dump($matches);
    
    ?>
    

    You don't need to iterate over the array, you can use preg_grep and pass your array instead.

    Happy coding

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部