tmh766 Posted February 17, 2007 Share Posted February 17, 2007 You guys helped me yesterday but I cant still can't figure out my problem What would you use to search an array for a substring and return the key its in? array_search($s1,$s2); what else Link to comment https://forums.phpfreaks.com/topic/38920-solved-super-quick-question/ Share on other sites More sharing options...
Psycho Posted February 17, 2007 Share Posted February 17, 2007 Well, you can use array_search() if you are looking for an exact match. But, if you want to find partial matches you will need to create a custom function. Link to comment https://forums.phpfreaks.com/topic/38920-solved-super-quick-question/#findComment-187168 Share on other sites More sharing options...
tmh766 Posted February 17, 2007 Author Share Posted February 17, 2007 So there is no simple predefined way to do it, I have to use this function Array_Search_Preg( $find, $in_array, $keys_found=Array() ) { if( is_array( $in_array ) ) { foreach( $in_array as $key=> $val ) { if( is_array( $val ) ) $this->Array_Search_Preg( $find, $val, $keys_found ); else { if( preg_match( '/'. $find .'/', $val ) ) $keys_found[] = $key; } } return $keys_found; } return false; } Link to comment https://forums.phpfreaks.com/topic/38920-solved-super-quick-question/#findComment-187179 Share on other sites More sharing options...
Orio Posted February 17, 2007 Share Posted February 17, 2007 It's pretty simple... <?php //returns an array with the keys that their values have the search string in them (can be an empty array) function sub_search($array, $search) { $results = array(); foreach($array as $key => $val) { if(strpos($val,$search) !== FALSE) $results[] = $key; } return $results; } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/38920-solved-super-quick-question/#findComment-187181 Share on other sites More sharing options...
tmh766 Posted February 17, 2007 Author Share Posted February 17, 2007 Thank you so much :) :) :) :) Link to comment https://forums.phpfreaks.com/topic/38920-solved-super-quick-question/#findComment-187184 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.