I have the following result in an array: Array ( [0] => Array ( [x_list_id] => 14 [client_id] => 6 [list_id] => 36 [x_list_name] => Chocolate [x_list_option] => choice [x_list_price] => 0 [x_list_status] => active [x_list_added] => 2007-10-09 17:20:35 ) [1] => Array ( [x_list_id] => 17 [client_id] => 6 [list_id] => 36 [x_list_name] => Cookies and Cream [x_list_option] => choice [x_list_price] => 0 [x_list_status] => active [x_list_added] => 2007-10-09 17:21:17 ) [2] => Array ( [x_list_id] => 13 [client_id] => 6 [list_id] => 36 [x_list_name] => Haagen-Dazs [x_list_option] => choice [x_list_price] => 0 [x_list_status] => active [x_list_added] => 2007-10-09 17:20:22 ) [3] => Array ( [x_list_id] => 16 [client_id] => 6 [list_id] => 36 [x_list_name] => Strawberry [x_list_option] => choice [x_list_price] => 0 [x_list_status] => active [x_list_added] => 2007-10-09 17:21:03 ) [4] => Array ( [x_list_id] => 15 [client_id] => 6 [list_id] => 36 [x_list_name] => Vanilla [x_list_option] => choice [x_list_price] => 0 [x_list_status] => active [x_list_added] => 2007-10-09 00:00:00 ) ) PHP: </DIV> and i am trying to find if some value exits using the in_array function but can get it to run properly: if(in_array("choice",$rows)){ $choice = "true";}if(in_array("standard",$rows)){ $standard = "true";} if(in_array("extra",$rows)){ $extra = "true";} PHP: what am I doing wrong here?
Are you sure you're building your array correctly? If what you put there is the code you are using, then the variable "$rows" doesn't actually exist. As far as I can tell your in_array code looks alright. That means the error is probably in how the array is being built.
this is how I bulid the array if ($nTotalRecsListX > 0) { $rows = array(); while($rowListX = phpmkr_fetch_assoc($rslistX)){ $rows[] = $rowListX;//build array list } //print_r($rows); } PHP:
Hmm... is the $row array being populated within a function? Or is it on the same level as the other code?
in_array() does not work recursively. Use this function instead. function in_array_recursive($needle, $haystack, $strict = false) { foreach ($haystack AS $hay) { if (is_array($hay)) { return in_array_recursive($needle, $hay, $strict); } else if ((!$strict AND $hay == $needle) OR ($strict AND $hay === $needle)) { return true; } } return false; } PHP:
this is what I did and it works fine. I was already looping through the arrays to write certain values but never came to me that i should do another loop to see if in_array. anyhow this is what i sis and it works: foreach($rows as $row){ if(in_array("choice",$row)){ $choice = "true";} if(in_array("standard",$row)){ $standard = "true";} if(in_array("extra",$row)){ $extra = "true";} } PHP: Thanks for help.