explode - loop to find all pieces

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

explode - loop to find all pieces

Post by JKM »

Hi there!

I am using a $piece = explode('.', $something); and I was wondering about how I make a loop that makes it unnecessary to do like this:

Code: Select all

if($piece[0] == $_POST['something'] || $piece[1] == $_POST['something'] || $piece[3] == $_POST['something']) {
    // something
}
.. When I don't know how many pieces I can end up with.
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Re: explode - loop to find all pieces

Post by ambivalent »

JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: explode - loop to find all pieces

Post by JKM »

Hmm, do I have to use a loop inside the loop (yo dawg!)?

The result I want, is like this: if($_POST['something'] isin explode('.', $fetch['something']) { //something }
But I'm quite blank, sorry.
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Re: explode - loop to find all pieces

Post by ambivalent »

Code: Select all

 
$pieces = array('one', 'two', 'three');
 
foreach ($pieces as $piece)
{
    if ($piece == 'two')
    {
        echo $piece;
    }
}
 
//output == two
 
Post Reply