php - Finding a part of string in an array of strings -
iv been looking way search through array of strings , find if there strings contain part of string, if there merge whole array onto array. eg:
searching jav: array->[java] -> return value , add whole array
after looking around on stackoverflow , testing found solution using strpos works...kind of:
$matches=array(); //just here show array declared , filled items prior calling foreach($myarray $fn){ if(strpos($fn,$searchstring)){ $matches = array_merge($myarray,$matches); } }
the issue is, instead of adding whole array adding search string each loop however, after double checking number of times dont understand why. tts adding original search item rather entire other array.... eg.
if search jav shown above array merge create array of correct length value :
javjavjavjavjav
any in understanding why or how correct appreciated.
the function strpos return false or 0, array matches not save word text jav.
this solution:
$myarray = ['java', 'java3', 'mouse', 'java2', 'keyboard']; $matches = []; foreach ($myarray $fn) { if (strpos($fn, 'jav') !== false) { $matches[] = $fn; } }
ps: don't know if understood correctly question, hope.
Comments
Post a Comment