Reply to Re: array_search() with preg_match?

Your name:

Reply:


Posted by Bob Winter on 11/09/03 11:19

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é
>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация