This question already has an answer here:
- PHP Associative Array Duplicate Keys 5 answers
How to allow php array to have duplicate keys? When I try to insert a key, value pair with already existing key it overwrites the value of corresponding previous key with the new value. Is there a way that I could maintain both duplicate keys having different values?
Can only be achieved through a multidimensional array
You could have a single key that has a value of an array(aka a multi-dimensional array), which would contain all the elements with that given key. An example might be
As porneL says, the whole point of arrays is that keys are unique.
If you want to reference multiple entries in an array then you need to search the array values.
This is obviously going to be more efficient if you maintain indexes. The code for this is not trivial.
If you're working with large datasets then I'd strongly recommend using a DBMS to manage the data. If that is not practical, then use a linked list.
PHP doesn't allow for this. The best solution is to use a multidimensional array. For instance...
Notice how I have duplicate keys named
key1
.Now if I want to call each instace of
key1
, runand it will return
I present you : Archive Array
Sample usage.
Class code
TLDR: Sometimes you need a hack like this to get the job done fast!
His question may have strong controversy, and goes against the teachings of computer science. (before you shoot, read the whole thing) But there are cases where you want this to happen. =X
For example, you have a code base, which manipulates a specified set of array objects. And due to its repeated usage (loops?, recursive?). It overrides or redefines the result. Till the final set is given.
And when you have everything all done. You suddenly realise your client (or your) specifications changed. Instead of the final data, you want every single data in between (hence wanting more then 1 data per key). And in the unfortunate case, your system was already completed in such a complicated way, it is a pain in the !@#$ to change everything to work with multi-dimensional array easily (meaning replace would not work, especially if you are using dynamic calls). So what do you do>??
This was actually a scenario i encounter recently, but there is a simple hack for this, that still ensures all your code still work, while still keeping the old data.
The end result, a class that can still be treated like any other object. But has gain an archive ability, to keep old data. It sorta a multi-dimensional array, with the [0] index accessed directly. And it works simply by changing the variable declaration with this object. And any changes made to the object parameter, would be archived. For easy access, with minimal or no change in the entire code program =)
It's not so much that "you can't do it". The downsides to an array with duplicate keys becomes apparent when you actually tried to use it.
$array['duplicate']
accesses you will only ever see the first entry.foreach
which sees each key/value pair regardless of the ambiguity.Anyway, to also have a verbatim answer to the question: you can use PHPs array syntax but have an accumulation object instead with: