Page 1 of 1

explode - loop to find all pieces

Posted: Sun Jun 21, 2009 5:51 am
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.

Re: explode - loop to find all pieces

Posted: Sun Jun 21, 2009 6:10 am
by ambivalent

Re: explode - loop to find all pieces

Posted: Sun Jun 21, 2009 6:47 am
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.

Re: explode - loop to find all pieces

Posted: Sun Jun 21, 2009 10:26 am
by ambivalent

Code: Select all

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