I once trying adding two values with the same key, but it didn't work. It overrode the old value. Isn't it possible to add more than one value with the same key, and when retrieving by key, I get a linked list which I can iterate to get all the different values?
2
-
So it DID work, because that's how it's supposed to be ;) Just like cdhowie below says, you can create (or possilbly find) a class that would have the functionality as you describe.Mchl– Mchl2010-12-20 10:23:02 +00:00Commented Dec 20, 2010 at 10:23
-
Your question title and question content are very different...BoltClock– BoltClock2010-12-20 11:02:03 +00:00Commented Dec 20, 2010 at 11:02
Add a comment
|
3 Answers
You can create a wrapper function:
function add_to_array($array, $key, $value) {
if(array_key_exists($key, $array)) {
if(is_array($array[$key])) {
$array[$key][] = $value;
}
else {
$array[$key] = array($array[$key], $value);
}
}
else {
$array[$key] = array($value);
}
}
So you just create a 2-dimensional array. You can retrieve the "linked list" (another array) by normal array access $array[$key].
Whether this approach is convenient is up to you.