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>";
		}
	}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.