See if value exists in array

Hi, I have an array that contains some values and I want to see if a certain value exists in the array.

The array is created like this:

	$sql = "SELECT pronumber,prochance from projects where protype = $var1 AND prochance = 3"; 
	$result = mysql_query($sql); 
	while($row = mysql_fetch_row($result))
		{ 
		$easyPro[$row[0]] = $row[1]; 
		} 
	
	//this gets a random project
	$randomeasyPro = array_rand($easyPro, 1);

If have tried using some sql with a case statement using the in() command however I really can’t crack it. Could someone help?

All I need to do is figure out is if the variable $checkPro appears in the pronumber part of the array and return 1 if it does and 0 if it doesnt to a variable called $appears

FYI this is the last thing I tried that didn’t work (please try no to laugh…):

$checkPro = '1';
$ids = join(',',$easyPro);  
 	$sql = "SELECT CASE $provalue WHEN IN ($ids) THEN 1 ELSE 0 END";

As an additional tiny thing - I can’t figure out when the way I’ve created the array demands that I include the prochance in the select part as I’m only using the values in the pronumber part of the array… It’s not the end of the world but it seems a waste to have unnecessary data stored! As you can see I really have no idea what I’m doing!

Thanks a lot for any suggestions.

SELECT * FROM table WHERE pronumber IN (1,2,3,4)

Thanks for your reply, however it’s setting the (1,2,3,4) to be the values from my array that is causing me problems. I don’t know what those values will be - I need to populate the IN() part with the data currently help in my array and then return a 1/0 based on that…

Thanks :slight_smile:

You have it above, implode(‘,’, $array);
var_dump or echo it out to see what is going wrong.

Sorry I got myself confused which means I’ve given you useless information. Thats what happens when I change my plan half way through writing something!

What I need to do is see if a value exists in the array, NOT get data from a db. Would explain why I was struggling!

So I have the array $easyPro and the variable $checkPro. All I need to do is find out if the value in $checkPro exists in the $easyPro array within the pronumber part. Then put a 1 into $appears if it does, and a 0 into $appears if it doesn’t. Best to ignore everything I wrote above, it is crap…

Sorry :frowning:

http://www.php.net/manual/en/ref.array.php
The one you seek is in_array() :slight_smile:

You can still use MySQL to check for $checkPro.

Your initial query:

SELECT pronumber, prochance
	FROM projects
		WHERE
			protype = $var1 AND prochance = 3

Add test for $proCheck (of course, PHP variables should be properly sanitized/escaped):

SELECT
	pronumber,
	prochance,
	FIELD(prochance, $checkPro) AS checkpro
		FROM projects
			WHERE
				protype = $var1 AND prochance = 3

While looping through the result set, the “checkpro” field will have a value of “1” where the “prochance” field has a matching value, and “0” otherwise. I’m certainly no SQL expert, though, so you may want to check the MySQL manual for ideas on MySQL-specific solutions (if you’re still curious, anyway).