Running "strpos" for each element of the array

I need to find out if a string exists within the value of an array, but is not necessarily the actual value of the array.

$array = array( 0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red' ); $key = xxxxxx('gr', $array); // $key = 2; 

Is there a built-in way to do this with PHP

+6
source share
10 answers

You can use preg_grep for this purpose

 <?php $array = array( 0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red' ); //$key = xxxxxx('gr', $array); // $key = 2; $result=preg_grep("/^gr.*/", $array); print_r($result); ?> 

Demo

+12
source

Function: array_filter is what you want. It only saves the elements in the resulting array when the specified function returns true for them.

 // return true if "gr" found in $elem // for this value function isGr($key, $elem) { if (strpos($elem, "gr") === FALSE) { return FALSE; } else { return TRUE; } } $grElems = array_filter($array, "isGr"); print_r($grElems); 

leads to:

 array( 2=>'green' ) 
+2
source

Your xxxxxx will be array_map . But alas, you cannot use strpos as a regular callback here. To pass the actual search parameter, you need to define a user-defined or anonymous function -

And to get what you want, you need to wrap even more:

  $key = key( array_filter( array_map( function($s){return strpos($s, "gr");}, $array ), "is_int") ); 

This gives you an index 2 search.

+2
source

how about foreach?

 function xxxxxx($str, $array) { foreach ($array as $key => $value) { if (strpos($value, $str) !== false) { return $key; } } return false; } 
+1
source

use

array_walk with strpos

array_walk

0
source

There is no built-in function, but since the preg_* functions support arrays as arguments, they should work:

 $keys = preg_filter('~gr~', '$0', $array); 
0
source

Scroll through the array and check each item, then return the index if that happens.

 function substr_in_array($array, $substr) { foreach ($array as $index => $item) { if (strrpos($item, $substr)!===FALSE && $item != $substr) { return $index; } } } 

This should do what you want.

0
source

It checks the foreach usage of each element of the array using strpos.

 <?php $array = array( 0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red' ); $search='aaa'; function arraysearch($array,$search){ foreach ($array as $element){ if(strpos($element,$search)!==FALSE){ return TRUE; } } return FALSE; } echo arraysearch($array,$search); ?> 
0
source

As of PHP 5.3, you can do this in a single command using array_filter and an anonymous function:

 $keys = array_filter($array, function($var){ return strpos($var, 'gr') !== false; }); 
0
source

Using PHP> = 5.3.0, you can use anonymous functions like callback to filter this array with array_filter .

 $array = array( 0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red' ); $search = 'gr'; $key = array_filter($array, function($element) use($search) { return strpos($element,$search) !== FALSE; }); 

This is an improved version of Shay Ben Moshe answer.

0
source

Source: https://habr.com/ru/post/887194/


All Articles