在数组中搜索部分值匹配 [英] Search for partial value match in an Array

查看:73
本文介绍了在数组中搜索部分值匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找给定此数组的函数

I'm looking for a function where given this array,

array(
 [0] =>
  array(
   ['text'] =>'I like Apples'
   ['id'] =>'102923'
 )
 [1] =>
  array(
   ['text'] =>'I like Apples and Bread'
   ['id'] =>'283923'
 )
 [2] =>
  array(
  ['text'] =>'I like Apples, Bread, and Cheese'
  ['id'] =>'3384823'
 )
 [3] =>
  array(
  ['text'] =>'I like Green Eggs and Ham'
  ['id'] =>'4473873'
 ) 
etc.. 

我要搜索针头

面包"

并获得以下结果

[1] =>
  array(
   ['text'] =>'I like Apples and Bread'
   ['id'] =>'283923'
 )
 [2] =>
  array(
  ['text'] =>'I like Apples, Bread, and Cheese'
  ['id'] =>'3384823'

推荐答案

使用 array_filter .您可以提供一个回调,该回调确定哪些元素保留在数组中以及哪些元素应删除. (从回调返回的值false表示应删除给定的元素.)类似这样的东西:

Use array_filter. You can provide a callback which decides which elements remain in the array and which should be removed. (A return value of false from the callback indicates that the given element should be removed.) Something like this:

$search_text = 'Bread';

array_filter($array, function($el) use ($search_text) {
        return ( strpos($el['text'], $search_text) !== false );
    });

有关更多信息:

这篇关于在数组中搜索部分值匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆