Page 1 of 1

Index of array where value not equal

Posted: Fri Jul 02, 2010 3:08 pm
by dimxasnewfrozen
I have a set of database values where all but one of the values will not equal 0.

I need to find the index of the array where this occurs (without doing a massive if statement)

Code: Select all

$levels = array($results["APP_S1_ID"][0],
		      $results["APP_S2_ID"][0],
		      $results["APP_S3_ID"][0],
		      $results["APP_S4_ID"][0],
		      $results["APP_S5_ID"][0]);
	
$l = array_search($levels, !=0); 
echo $l;

Re: Index of array where value not equal

Posted: Fri Jul 02, 2010 3:47 pm
by AbraCadaver
You need to give an actual var_dump() of your array. Also, only one value is 0 or only one value is NOT 0, and by NOT 0, does that mean greater or less than zero, or some other string value or what?

Just for fun if you know there is only one non zero:

Code: Select all

$l = current(array_diff(array_flip($levels), array_keys($levels, 0)));
If you're looking for not zero, not empty string, not false, not null:

Code: Select all

$l = array_search(true, $levels);