Re: array_search() with preg_match?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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é


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux