doulangbizhan5160 2013-10-23 08:53 采纳率: 100%
浏览 100
已采纳

使用php在数组中查找关键字?

I have one keyword like this format

sample text

Also i have one array like this following format

Array
(
   [0] => Canon sample printing text
   [1] => Captain text
   [2] => Canon EOS Kiss X4 (550D / Rebel T2i) + Double Zoom Lens Kit
   [3] => Fresh sample Roasted Seaweed
   [4] => Fresh sample text Seaweed
)

I want to find sample text keyword in this array. My expected result

Array
    (
       [0] => Canon sample printing text        //Sample and Text is here
       [1] => Captain text             //Text is here
       [3] => Fresh sample Roasted Seaweed       //Sample is here
       [4] => Fresh sample text Seaweed          //Sample text is here
    )

I am already trying strpos but its not getting correct answer

Please advise

  • 写回答

2条回答 默认 最新

  • douxi4114 2013-10-23 09:03
    关注

    A simple preg_grep will do the job:

    $arr = array(
        'Canon sample printing text',
        'Captain text',
        'Canon EOS Kiss X4 (550D / Rebel T2i) + Double Zoom Lens Kit',
        'Fresh sample Roasted Seaweed',
        'Fresh sample text Seaweed'
    );
    $matched = preg_grep('~(sample|text)~i', $arr);
    print_r($matched);
    

    OUTPUT:

    Array
    (
        [0] => Canon sample printing text
        [1] => Captain text
        [3] => Fresh sample Roasted Seaweed
        [4] => Fresh sample text Seaweed
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效