whizzfrapp Posted May 16, 2009 Share Posted May 16, 2009 Is there any way to use the keys that i want in a keyed array? I'm doing this but it gives me an error $result = db_query('SELECT * FROM {team}'); $rows = array(); while ($team = db_fetch_object($result)) { array_push($rows, $team); } $check = array(); foreach($rows as $team) { $id = $team->team_id; $name = $team->name; array_push($check, $id => t($name)); } Link to comment https://forums.phpfreaks.com/topic/158427-help-with-array/ Share on other sites More sharing options...
Masna Posted May 16, 2009 Share Posted May 16, 2009 Modify your foreach loop: foreach($array as $key=>$value){ //you can use $key here as the key of each element in $array } Link to comment https://forums.phpfreaks.com/topic/158427-help-with-array/#findComment-835532 Share on other sites More sharing options...
whizzfrapp Posted May 16, 2009 Author Share Posted May 16, 2009 Thank you. Do you mean like this ? $result = db_query('SELECT * FROM {ge_team}'); $rows = array(); while ($team = db_fetch_object($result)) { array_push($rows, $team); } $check = array(); foreach($rows as $key=>$value) { $key = $team->team_id; $value = $team->name; array_push($check, $key=>$value); } Link to comment https://forums.phpfreaks.com/topic/158427-help-with-array/#findComment-835538 Share on other sites More sharing options...
Ken2k7 Posted May 16, 2009 Share Posted May 16, 2009 What are you trying to do with the second array_push()? Link to comment https://forums.phpfreaks.com/topic/158427-help-with-array/#findComment-835539 Share on other sites More sharing options...
whizzfrapp Posted May 16, 2009 Author Share Posted May 16, 2009 I'm working on drupal and I want to have a set of checkboxes related to the teams that exist in the database. And that array of checkboxes should be in this form: array($team_id => $team_name, $team_id => $team_name) so that when I click the checkboxes that i want, those teams be written in the database. Link to comment https://forums.phpfreaks.com/topic/158427-help-with-array/#findComment-835541 Share on other sites More sharing options...
Ken2k7 Posted May 16, 2009 Share Posted May 16, 2009 Don't use array_push() then. Do this - $check[$key] = $value; Simple as that. Link to comment https://forums.phpfreaks.com/topic/158427-help-with-array/#findComment-835543 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.