Lucky2710 Posted August 14, 2010 Share Posted August 14, 2010 I have an assoc array thats pulled from mysql. Heres the array... Array ( [0] => Array ( [iD] => 288 [user_ID] => 61 [Game_ID] => 12 [Week] => 1 [Pick] => 1 ) [1] => Array ( [iD] => 287 [user_ID] => 61 [Game_ID] => 11 [Week] => 1 [Pick] => 2 ) [2] => Array ( [iD] => 286 [user_ID] => 61 [Game_ID] => 10 [Week] => 1 [Pick] => 2 ) [3] => Array ( [iD] => 285 [user_ID] => 61 [Game_ID] => 9 [Week] => 1 [Pick] => 2 ) [4] => Array ( [iD] => 284 [user_ID] => 61 [Game_ID] => 8 [Week] => 1 [Pick] => 1 ) [5] => Array ( [iD] => 283 [user_ID] => 61 [Game_ID] => 7 [Week] => 1 [Pick] => 1 ) [6] => Array ( [iD] => 282 [user_ID] => 61 [Game_ID] => 6 [Week] => 1 [Pick] => 2 ) [7] => Array ( [iD] => 281 [user_ID] => 61 [Game_ID] => 5 [Week] => 1 [Pick] => 1 ) [8] => Array ( [iD] => 280 [user_ID] => 61 [Game_ID] => 4 [Week] => 1 [Pick] => 2 ) [9] => Array ( [iD] => 279 [user_ID] => 61 [Game_ID] => 3 [Week] => 1 [Pick] => 2 ) [10] => Array ( [iD] => 278 [user_ID] => 61 [Game_ID] => 2 [Week] => 1 [Pick] => 1 ) [11] => Array ( [iD] => 277 [user_ID] => 61 [Game_ID] => 1 [Week] => 1 [Pick] => 2 ) ) I need to search the arrays inside the main array, to look for where "Pick = 1" if so then echo Away, and some info out of that array. Or if "Pick = 2" then echo Home and some info from that array. And i need it to do foreach array inside that array. I need to search this array. Heres my code so far... $user = 61; $query = " SELECT * FROM CollegeFootballPicks WHERE CollegeFootballPicks.User_ID = $user"; $result = mysql_query($query); $affected = mysql_num_rows($result); echo $affected; echo '<br />'; for($i=0; $i<$affected; $i++){ $picks[] = mysql_fetch_assoc($result); } foreach($picks as $pic) if($pic['Pick'] == 1) echo $pic['User_ID'];echo '<br />'; echo $pic['Game_ID'];echo '<br />'; echo $pic['Week'];echo '<br />'; echo 'Away';echo '<br />';echo '<br />'; foreach($picks as $pic) if($pic['Pick'] == 2) echo $pic['User_ID'];echo '<br />'; echo $pic['Game_ID'];echo '<br />'; echo $pic['Week'];echo '<br />'; echo 'Home';echo '<br />';echo '<br />'; But this isn't working. But i have no errors. Does anyone know how i can search the nested array. Quote Link to comment https://forums.phpfreaks.com/topic/210717-searching-nested-arrays/ Share on other sites More sharing options...
AbraCadaver Posted August 14, 2010 Share Posted August 14, 2010 Much simpler: while($pic = mysql_fetch_assoc($result)) { echo $pic['User_ID'].'<br />'; echo $pic['Game_ID'].'<br />'; echo $pic['Week'].'<br />'; if($pic['Pick'] == 1) { echo 'Away<br /><br />'; } if($pic['Pick'] == 2) { echo 'Home<br /><br />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/210717-searching-nested-arrays/#findComment-1099209 Share on other sites More sharing options...
PFMaBiSmAd Posted August 14, 2010 Share Posted August 14, 2010 I've been following some of your threads while you attempt to create this application. Storing just an away/home indicator as the pick value makes it extra complicated to associate that back to the actual team. Is there some reason you did not store the team id that they picked as the pick value? You have added an extra level of abstraction by just using an away/home value. Quote Link to comment https://forums.phpfreaks.com/topic/210717-searching-nested-arrays/#findComment-1099210 Share on other sites More sharing options...
Lucky2710 Posted August 14, 2010 Author Share Posted August 14, 2010 Because it was quicker to create to begin with, and made the code way shorter, because instead of on the picks page having to specify each team for each game, all i had to do was say 1 = away and 2 = home. At the time it seemed like the best way to go. And now trying to make it user friendly to show them what they picked. Now its gonna be just as complicated, and long. But i'm not re doing all the stuff i've done so far. So im just gonna keep going with how it is. This thread, i had a code error. Problem solved! my if statements i forgot the { & }. Quote Link to comment https://forums.phpfreaks.com/topic/210717-searching-nested-arrays/#findComment-1099215 Share on other sites More sharing options...
Lucky2710 Posted August 14, 2010 Author Share Posted August 14, 2010 Thanks AbraCadaver Thats way shorter and does the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/210717-searching-nested-arrays/#findComment-1099221 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.