galayman Posted November 29, 2007 Share Posted November 29, 2007 Hi all, i have this array $str = array("apple is red", "banana is yellow", "pineaple is tropical fruit", "coconut is also tropical fruit", "grape is violet"); i want to find string "coconut" inside $str and put the whole string "coconut is also tropical fruit" into variable $coconut. can i do this? i don't want to use the this code <? $coconut = $str[3]; echo "$coconut"; ?> can someone help? Thank you, Best Regards Link to comment https://forums.phpfreaks.com/topic/79456-solved-find-a-string-in-array/ Share on other sites More sharing options...
pocobueno1388 Posted November 29, 2007 Share Posted November 29, 2007 Give this a try <?php $str = array("apple is red", "banana is yellow", "pineaple is tropical fruit", "coconut is also tropical fruit", "grape is violet"); foreach ($str as $phrase){ if (strstr($phrase, 'coconut')) $coconut = $phrase; if(isset($coconut)) break; } echo $phrase; ?> Link to comment https://forums.phpfreaks.com/topic/79456-solved-find-a-string-in-array/#findComment-402334 Share on other sites More sharing options...
galayman Posted November 29, 2007 Author Share Posted November 29, 2007 waw! there is a lot of php guru here, and they are a nice person too, help me only in a few minute, i hope i have e money and can donate to php freaks thank you. TOPIC SOLVED! Link to comment https://forums.phpfreaks.com/topic/79456-solved-find-a-string-in-array/#findComment-402337 Share on other sites More sharing options...
pocobueno1388 Posted November 29, 2007 Share Posted November 29, 2007 Your welcome I did make a mistake though. In my code, change echo $phrase; To echo $coconut; $coconut is the variable that will hold the correct phrase. Link to comment https://forums.phpfreaks.com/topic/79456-solved-find-a-string-in-array/#findComment-402340 Share on other sites More sharing options...
galayman Posted November 29, 2007 Author Share Posted November 29, 2007 once again thank you, you are a nice person pocobueno1388, i hope good things happen to good people like you. Link to comment https://forums.phpfreaks.com/topic/79456-solved-find-a-string-in-array/#findComment-402348 Share on other sites More sharing options...
UD2008 Posted October 13, 2011 Share Posted October 13, 2011 I know this is an old topic, but I just found and wish to reply on it. The code works for my great, but I have a question. - What if the array has multiple of the same string. How can I display them all? Thanks Link to comment https://forums.phpfreaks.com/topic/79456-solved-find-a-string-in-array/#findComment-1278920 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.