Filter multidimensional array based on partial match of search value

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:

  • array_filter
  • strpos return values

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',
  ),
)

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

 }