Re: array_search() with preg_match?
| From: | Bob Winter | Date: | Thu, 23 Jun 2005 17:40:03 +0000 |
| Subject: | Re: array_search() with preg_match? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-217505@lists.php.net to get a copy of this message | ||
René,
This may not the most efficient method, but works:
$array = array('this sky is blue', 'pencils are orange', 'I like green apples','strawberries are red');
$search = 'green';
$result = array();
foreach($array as $key1=>$value1) {
if(substr_count($value1, $search)) {
$result[] = $key1;} } // $result is an array containing all the keys for the values that contained the search phrase print_r($result); // or convert to string print implode(' ', $result); // or however else to need to handle the results --Bob René Fournier wrote:
I need to search an array for a string, with a twist. E.g.: $array = array(0 => 'this sky is blue', 1 => 'pencils are orange', 2 => 'I like green apples', 3 => 'strawberries are red'); $key = array_search('green', $array); With the above code, nothing will be returned, but I would like it to return 2 for the key containing "green". Any suggestions? ...René