Page 1 of 1

Help with in_array

Posted: Wed Oct 10, 2007 12:17 pm
by louie35
I have the following result in an array:

Code: Select all

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 ) )
and i am trying to find if some value exits using the in_array function but can get it to run properly:

Code: Select all

if(in_array("choice",$rows)){ $choice = "true";}if(in_array("standard",$rows)){ $standard = "true";}if(in_array("extra",$rows)){ $extra = "true";}
Why is this not working?

Posted: Wed Oct 10, 2007 12:24 pm
by John Cartwright
First of all, open up your browser, view the source of the file, then post your print_r() of the array from that window (so we can read it)

Secondly, put a little whitespace in your php-code. I can barely read that either.

Once those are fixed I will happily help you :)

No, I'm not being an arse, just helping you help us, so we can help you ;)

Posted: Wed Oct 10, 2007 12:28 pm
by feyd
Can I assume $rows is the entire print_r() output above? If so, in_array() won't find 'choice' or any other string. All of $rows' elements are arrays.

Posted: Wed Oct 10, 2007 12:31 pm
by louie35
SO what's the right code for this?

I can never get my head around arrays.

Posted: Wed Oct 10, 2007 12:39 pm
by feyd
louie35 wrote:SO what's the right code for this?

I can never get my head around arrays.
It would probably be a loop of some sort. Unfortunately, the context of the code you posted isn't known.

Posted: Wed Oct 10, 2007 1:07 pm
by s.dot
Yes, you will need to loop through the main array and check each sub-array's value.

Code: Select all

foreach($mainArray AS $subArray)
{
    if (in_array('some value', $subArray))
   {
        //do something
    }
}
Although this is probably not the best approach, considering you've stuck all $rows into one array. I don't know why you would want to do that (of course I don't know the context of the code either).

Posted: Wed Oct 10, 2007 2:10 pm
by louie35
because i was stupid.
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:

Code: Select all

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";}
			}
Thanks for help.