Jump to content

[SOLVED] Array help, if statement not working?


kernelgpf

Recommended Posts

Alright, my script takes a list from a database:

 

apple | red | juicy, orange | orange | fleshy, grapefruit | pink | fleshy

 

explodes between the commas making

 

apple | red | juicy

orange | orange | fleshy

grapefruit | pink | fleshy

 

and then runs a foreach on the exploded parts, and further explodes it between the bar things ( | ).

 

So it then becomes

 

apple | red | juicy

[0] apple

[1] red

[2] juicy

 

orange | orange | fleshy

[0] orange

[1] orange

[2] fleshy

 

grapefruit | pink | fleshy

[0] grapefruit

[1] pink

[2] fleshy

 

beautiful. then I take the first part of the second explosion. [possible values are apple, orange, or grapefruit], and try to match it up with the variable "$fruit".

 

I was running into problems, so I made it print out the first part of the second explosion and made it print out $fruit next to it. If the two matched up, "MATCH" should print. now, MATCH ONLY prints when "apple" (or $fruit) is FIRST in the first explosion.

 

So my script works if $row[biglistoffruit] equals "apple | red | juicy, orange | orange | fleshy" but not "orange | orange | fleshy, red | juicy | apple." I need it to work regardless of where it is in the first explosion.

 

Script:

	$fruit="apple";
$fruitarray = explode(",", $row[biglistoffruit]);



	foreach ($fruitarray as $value){
		print "$value<br>";
		//check if this part of the array has the fruit
		$fruitinfo=explode(" | ",$value);
		print "if $fruitinfo[0] equals $fruit<p>";
		if($fruitinfo[0] == $fruit){ //if this matches
			print " - MATCH!<p>";
		}
	}

Try

<?php
if(trim($fruitinfo[0]) == $fruit){
   print " - MATCH!<p>";
}

if that works, you know your problem -- evil whitespace

 

If that has nothing to do with your problem, then it's because I totally don't understand what your question is.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.