Can associative arrays contain identical keys? I need some sort of PHP container that will hold 2 id numbers. Either of those ID numbers could be duplicated multiple times, but its the pair of IDs that makes the instance unique. What is my best option? Thanks Mike
@ShortsMar 29.2010 — #Not sure why you need duplicate ids, since ids are made to be for a single itemelement. Otherwise you'd probably be looking at groupsclasses.
In that case, going with grouping, if things are going to have the same group idea, you can go along with having that array key being an array itself.
[code=php]<?php $array = array( 'id_1' => '$array["id_1"] is the only item in this group.', 'id_2' => array( '$array["id_2"][0] is an item in this group.', '$array["id_2"][1] is also an item in this group.' ) ); ?>[/code]
@dmikester1authorMar 29.2010 — #Wow, thats kind of confusing. The reason I can have multiple IDs for this part is that each ID relates to a given athlete or a given event. I'm listing all the times each athlete is entered into each event on my page. Each athlete could be entered more than once and each event could be entered more than once. I need to store the athlete->event pairs in something to send through my form. I am storing an associative array in a session variable, but its not quite working.
@aj_nscMar 29.2010 — #So pick either the athlete or the event and use that as your primary key. Then keep track of the others in an array belonging to the first.
Example: Athletes 1, 2, and 4 are each entered into events 1, 7, and 9, with athlete 3 only in event 9
@MindzaiMar 29.2010 — #To answer the question, no you cannot. As suggested you would need to use some other data structure, and I would agree that it sounds like an multidimensional array is probably your best bet. They can seem confusing at first by my advice is get used to them, because they crop up a lot in PHP!
If you need to maintain the array structure for your form that's no problem, just name the form elements accordingly. Assuming you want to have an array of events, and for each event an array of athletes, you might do something like this:
So you have an array of events with the event ids as keys. Each element of this array is an array of athlete ids. So event 1 has athlete 123 and athlete 234 etc. This sort of structure is fairly commonplace as it lets you traverse the data effectively:
@dmikester1authorMar 29.2010 — #Awesome, thanks guys. This is starting to make a lot more sense now. One more question. I need to create an almost identical array on the first page with the form and send that over in the session. How do I create an identically-structured array in PHP as you showed me how to do in the form. So this one in PHP would have all the athletes and events so I have an array to compare the checkboxes against on the other side.
@ShortsMar 30.2010 — #Well, $_SESSION itself is able to be an associative array.
So depending on what's in $_SESSION['events'] could build your list.
[code=php]foreach($_SESSION['events'] as $event_id => $athletes){}[/code]
You'd just have to make sure you initiate the session on each page session_start(); and go from there. When submitting the form, store all the data there and then retrieve it on a later page. Although you'll lose that data once they leave so you'd still need to store it somewhere (MySQL mostlikely).
@dmikester1authorMar 30.2010 — #Hmm, I'm still having a problem. I am getting the original data from a mysql database so I'm populating the PHP array in a [code=php]while($row = mysql_fetch_assoc($result)) {[/code] loop. It appears like i'm writing over data each time it finds an identical key, so I'm not quite populating that multi-dimensional array correctly. I'm pretty confused about how to add to a multi-dimensional array. I read that you should not use array_push.