Digital Point Forums
Send Money Online

Go Back   Digital Point Forums > Design & Development > Programming > PHP
Google Analytics
Log In to view
your analytics

Reply
 
Thread Tools
  #1  
Old Feb 29th 2008, 1:51 pm
jayg5000 jayg5000 is offline
Champion of the Naaru
 
Join Date: Jun 2006
Posts: 224
jayg5000 is on a distinguished road
More flexibility in searching an array

I am trying to search an array to see if it contains a certain word but the in_array(); function (form what I can see) needs to be the exact value of the array values. Here is an example:

Code:
$fruit = array('Green Apple', 'Yellow Banana', 'Red Cherry');
How would I search $fruit to see if it contains the word "Green" or "Cherry?" Maybe I'm thinking so hard I can't see the simple solution. Thanks for any help.
Reply With Quote
  #2  
Old Feb 29th 2008, 6:26 pm
RoscoeT's Avatar
RoscoeT RoscoeT is offline
Grunt
 
Join Date: Jan 2008
Posts: 64
RoscoeT is on a distinguished road
Thumbs up

You could use foreach and loop through the array preforming a strpos on each element.

Try something like this untested copied and pasted junk...

php Code:
$fruit = array('Green Apple', 'Yellow Banana', 'Red Cherry');

foreach ($fruit as $mystring){
   $findme   = 'Green';
   $pos = strpos($mystring, $findme);

   // Note our use of ===.  Simply == would not work as expected
   // because the position of 'a' was the 0th (first) character.
   if ($pos === false) {
       echo "The string '$findme' was not found in the string '$mystring'";
     } else {
       echo "The string '$findme' was found in the string '$mystring'";
       echo " and exists at position $pos";
   }
}
Reply With Quote
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Full Time PHP Coder Position - Salary + Paid Holidays + Flexibility LeeD Programming 0 Jan 3rd 2008 1:15 pm
Add array in array recursively? nljeffrey PHP 3 Oct 26th 2007 7:02 am
searching a page for links and putting links into array - beginner angrypenguin PHP 2 Sep 1st 2007 10:12 am
Array in array [help ! urgent] er1cw PHP 9 Oct 9th 2006 10:03 am
Array problem: function.reset: Passed variable is not an array or object beniwtv@hotmail.com PHP 1 Mar 2nd 2006 6:20 pm


All times are GMT -8. The time now is 11:40 pm.