34

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.. 

I want to search for the needle

"Bread"

and get the following result

[1] =>
  array(
   ['text'] =>'I like Apples and Bread'
   ['id'] =>'283923'
 )
 [2] =>
  array(
  ['text'] =>'I like Apples, Bread, and Cheese'
  ['id'] =>'3384823'
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Chamilyan
  • 9,347
  • 10
  • 38
  • 67

3 Answers3

59

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 );
    });

For more information:

Jon Gauthier
  • 25,202
  • 6
  • 63
  • 69
  • 2
    Better use `strpos(…) !== FALSE`. This saves a function call and this is faster. – Shi Aug 03 '11 at 20:00
  • Thanks Hans, out of curiosity what is the 'use' operator? Is it like the 'as' operator in an each loop? I can't find any info about it. – Chamilyan Aug 03 '11 at 21:41
  • 6
    The `use` keyword makes the variables you give it available in the function's scope. By default, inside that function `$search_text` would be undefined, and so we write `use` to have PHP "carry over" the variable into the local scope. – Jon Gauthier Aug 03 '11 at 22:09
  • Why do I keep getting this error? `Parse error: syntax error, unexpected T_FUNCTION` I even tried copying the example straight from [link](http://www.php.net/manual/en/function.array-filter.php#100813) – gavsiu Apr 07 '13 at 04:17
  • @gavsiu http://stackoverflow.com/questions/6412032/php-anonymous-function-causes-syntax-error-on-some-installations – Jon Gauthier Apr 07 '13 at 15:09
  • @JonGauthier How to find text but return id? – Senior Pomidor Oct 24 '22 at 12:43
3

From PHP8, there is a new function to return a boolean value to express whether a substring occurs in a string (this is offered as a simpler replacement for strpos()).

str_contains()

This would need to be called within an iterating function/construct.

From PHP7.4 arrow functions can be used reduce the overall syntax and invite global variables into the custom function's scope.

Code: (Demo)

$array = [
    ['text' => 'I like Apples', 'id' => '102923'],
    ['text' => 'I like Apples and Bread', 'id' =>'283923'],
    ['text' => 'I like Apples, Bread, and Cheese', 'id' => '3384823'],
    ['text' => 'I like Green Eggs and Ham', 'id' =>'4473873']
];

$search = 'Bread';
var_export(
    array_filter($array, fn($subarray) => str_contains($subarray['text'], $search))
);

Output:

array (
  1 => 
  array (
    'text' => 'I like Apples and Bread',
    'id' => '283923',
  ),
  2 => 
  array (
    'text' => 'I like Apples, Bread, and Cheese',
    'id' => '3384823',
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
1

is there a reason for multi array. is id unique and can it be used as index.

$data=array(

  array(
   'text' =>'I like Apples',
   'id' =>'102923'
 )
,
  array(
   'text' =>'I like Apples and Bread',
   'id' =>'283923'
 )
,
  array(
  'text' =>'I like Apples, Bread, and Cheese',
  'id' =>'3384823'
 )
,
  array(
  'text' =>'I like Green Eggs and Ham',
  'id' =>'4473873'
 )

 );

$findme='bread';

 foreach ($data as $k=>$v){

 if(stripos($v['text'], $findme) !== false){
 echo "id={$v[id]} text={$v[text]}<br />"; // do something $newdata=array($v[id]=>$v[text])
 }

 }
amigura
  • 139
  • 5