Closed Thread Icon

Topic awaiting preservation: Explode() and foreach() help Pages that link to <a href="https://ozoneasylum.com/backlink?for=12909" title="Pages that link to Topic awaiting preservation: Explode() and foreach() help" rel="nofollow" >Topic awaiting preservation: Explode() and foreach() help\

 
Author Thread
Synthetic
Paranoid (IV) Inmate

From: under your rug,
Insane since: Jul 2001

posted posted 09-29-2003 07:47

Ok i'm trying to explode a variable and check if another variable entry is in the first one.

For exampe...

$variable1 = '1,2,4,5,7,8';

$variable2 = '5';

I need to check if the 5 of variable2 is in the variable1 array. So i've tried different variations like this, but none seem to be correct. I tried the manual but it wasn't much help.

code:
$incomming = explode(',', $variable1);
foreach( $variable1 AS $eachone){
if ($eachone != variable2){
print 'variable2 is not in variable1';
}
else{
print 'variable2 is in variable1';
}
}



Am I on the right track? If so, why it not work, if not, kick me the right way

Synthetic's Chess Player Page

[This message has been edited by Synthetic (edited 09-29-2003).]

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 09-29-2003 08:49

This would be the one-line solution

$var_exists = in_array($variable2, explode(',', $variable1));

BTW1 Take a look at in_array() function definition, it can be called with additional parameter which will also check if variables are the same type.

BTW2 If both variables that you're using are strings, a simple call to strpos() function would be even more effective...


Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-29-2003 08:55

Ew, who was in charge of naming the function "explode" when it's always named "split" in every other language?

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 09-29-2003 09:22

Slime, PHP has both explode() and split() functions... The difference is that explode() splits a string by string while split() splits a string by regular expression.

Oh, and for the record, PHP also has both implode() and join() functions defined, but in this case join() is an alias for implode() and therefore they are the same thing.


Synthetic
Paranoid (IV) Inmate

From: under your rug,
Insane since: Jul 2001

posted posted 09-29-2003 20:11

*Synthetic wounders when the last time he mentioned how much he loved mr.maX*

Anyway lol, thanks again mr.maX, that bit was exactly what I needed

Synthetic's Chess Player Page

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-29-2003 20:37

OK then, that's not quite so bad =)

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 10-01-2003 04:46

Also, "explode" is a much more entertaining mental image.

Cell 1250 :: alanmacdougall.com :: Illustrator tips

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 10-01-2003 12:39

The explode + in_array combo sounds obvious but I prefer by far mr.maX BTW2 suggestion.

Mathieu "POÏ" HENRI

« BackwardsOnwards »

Show Forum Drop Down Menu