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 Quote Link to comment 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; ?> Quote Link to comment 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! Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.