/    Sign up×
Community /Pin to ProfileBookmark

can associative arrays contain identical keys?

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

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@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]
Copy linkTweet thisAlerts:
@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.

Thanks

Mike
Copy linkTweet thisAlerts:
@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

[code=php]
<?php
$events = array(
1 => array(
1,
2,
4
),
7 => array(
1,
2,
4
),
9 => array(
1,
2,
3,
4
)
);
?>
[/code]
Copy linkTweet thisAlerts:
@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:

[code=html]<input type="checkbox" name="events[1][]" value="123" />
<input type="checkbox" name="events[1][]" value="234" />
<input type="checkbox" name="events[2][]" value="345" />
<input type="checkbox" name="events[3][]" value="456" />[/code]


Which would result in an array structure like:

<i>
</i>$_POST =&gt; array(
'events' =&gt; array(
1 =&gt; array(
0 =&gt; 123,
1 =&gt; 234
),
2 =&gt; array(
0 =&gt; 345
),
3 =&gt; array(
0 =&gt; 456
)
)
)


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:

[code=php]
foreach ($_POST['events'] as $event_id => $athletes) {
echo "<h1>Event #$event_id</h1>";
echo "<ul>"
foreach ($athletes as $athlete_id) {
echo "<li>Athlete #$athlete_id</li>";
}
echo "</ul>";
}[/code]


Would result in:

[code=html]
<h1>Event #1</h1>
<ul>
<li>Athlete #123</li>
<li>Athlete #234</li>
</ul>
<h1>Event #2</h1>
<ul>
<li>Athlete #345</li>
</ul>
<h1>Event #3</h1>
<ul>
<li>Athlete #456</li>
</ul>[/code]


Obviously this is a simple example, you will often want to store more data in your arrays, but hopefully that's made it a bit clearer and not worse!
Copy linkTweet thisAlerts:
@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.

Thanks.

Mike
Copy linkTweet thisAlerts:
@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).
Copy linkTweet thisAlerts:
@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.

Thanks

Mike
Copy linkTweet thisAlerts:
@ShortsMar 30.2010 — [code=php]
$events = array();
while($row = mysql_fetch_assoc($result)):
$events[$row['event_id']][] = $row['athlete_id'];
endwhile;
$_SESSION['events'] = $events;
[/code]

Or if it's all just going into $_SESSION without validation.
[code=php]
while($row = mysql_fetch_assoc($result)):
$_SESSION['events'][$row['event_id']][] = $row['athlete_id'];
endwhile;
[/code]


That would be assuming the $row is getting event and athlete from the SQL. The [] will add to the array instead of overwriting a single value.
×

Success!

Help @dmikester1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...