PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
louie35
Forum Contributor
Posts: 144 Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:
Post
by louie35 » Wed Oct 10, 2007 12:17 pm
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?
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Wed Oct 10, 2007 12:24 pm
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Oct 10, 2007 12:28 pm
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.
louie35
Forum Contributor
Posts: 144 Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:
Post
by louie35 » Wed Oct 10, 2007 12:31 pm
SO what's the right code for this?
I can never get my head around arrays.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Oct 10, 2007 12:39 pm
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.
s.dot
Tranquility In Moderation
Posts: 5001 Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana
Post
by s.dot » Wed Oct 10, 2007 1:07 pm
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).
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
louie35
Forum Contributor
Posts: 144 Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:
Post
by louie35 » Wed Oct 10, 2007 2:10 pm
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.