All the functions described above
presume that, in the two arrays passed as arguments (or in the only one dealt with by
arraySwap)
all the values and all the keys in both arrays are
unique and have no duplicates.
If a
duplicate would be present, upon reassigning it as a
key (like in
arrayCombine where you reassign as keys of
array1 its very same
values, which might well carry duplicates) would immediately
overwrite any possible previously defined
key that carried that same identifier.
That is, whereas in an array values can be duplicated, yet the
keys of an array must be
unique (in fact this is why they are so often referred to as
keys: for they are fit for and meant to identifiy one "
gate" only) and thus, if a key duplicate is met upon assigning it to the pool of keys already present in an array, the last arrived key
overwrites the previous one(s): there will
not be two identical
keys in the array, though there can be more duplicated
values yet
held by different keys:
array[0]="hi";
array[2]="hi";
this can be: two identical
values but on two different
keys
But it could
not be viceversa: if
array[0]="hi"
exists and later arrives
array[0]="hello"
Ther result will be one and only one (and
not two!)
array[0]
entry, and it will be the last one arrived namely the one carrying the value
"hello" that will
overwrite any previous instance of... itself.
Though I won't let you play with the following in the test form, yet I am to provide you with a set of functions that are equivalent to the previous ones and that yet can
accommodate these cases, namely
duplicates.
The codes of these functions are in the textarea that carries all of the array combiner codes, though they are
not included within the set of functions you can
trigger in the
test form.
These new functions are named as follows:
- arrayCombineX is the same as arrayCombine but accommodates duplicates.
- arrayCombineX2 is the same as arrayCombine2 but accommodates duplicates.
- arrayCombineX3 is the same as arrayCombine3 but accommodates duplicates.
- arrayCombineX4 is the same as arrayCombine4 but accommodates duplicates.
- arraySwapX is the same as arraySwap but accommodates duplicates.
The way this gets achieved is the following one: return an array whose
each entry is always an array itself
of at least one entry (for single matches) or of multiple entries (for
duplicated matches).
This I deem the only "bullet proof" way to handle these situations - and for
various considerations, especially when keeping in mind that whereas
PHP allows only String or Number data types to perform as keys of an array,
Javascript allows
everything included even Objects.
And if you consider that an assigned value could be an array
originally (an array can well have as its values
other arrays, or Objects!), then checking whether the
typeof of an entry is an array data type with the idea of veryifing whether it included more matches or not, is
no longer a safe way to check that case: so
all of the entries must be arrays, and these
latter can
then safely hold all the rest, included other arrays if they were met in the arrays
passed as
arguments.
So the "bullet proof" method is the mentioned one: the
returned recombined array is an array whose keys are unique, but
the values held are always an array themselves and of at least one entry: one entry alone if there was
no duplicate,
or more than one entries if it had to
accommodate duplicates.
Example: you have two arrays like:
array1=[0,1,2,3,4,5];
array2=[0,1,1,1,2,7];
If you want to use the values of array2 (which carries three
duplicated number 1) as keys in the newly
recombined array, and the values of array1 as the newly
recombined array values, you'd get a structure as follows (
pseudo javascript code):
result[0]=array(0);
result[1]=array(1,2,3);
result[2]=array(4);
result[7]=array(5);
such would be the result of a call like:
var result = arrayCombineX( array2, array1 );
Note: please keep in mind that javascript may produce apparent empty slots if the keys are numbers and there are gaps between the sequence of such keys (such as 7, then 12: 4 numbers missing namely 8, 9, 10, 11) - an interesting answer about this javascript behaviour by Mr. Danny Goodman can be located clicking here.
Therefore in the returned arrays of the
X versions, there
always exists at least a:
result[index][0]
entry, and then there
could be, beyond that, more subsequent entries to host the
possible duplicates such as like:
result[index][0+num]
So when dealing with the returned
outcome of the
X versions of these functions: check the
length!
if(result[index].length==1){/*no duplicates*/}
else{/*duplicates present!*/}