I have a dictionary array like below
$foo = array(
'key1' => 'value 1',
'key1' => 'value 2',
'key2' => 'value 3',
'key2' => 'value 4',
'key3' => 'value 5'
);
as you can see there are duplicate keys. All keys are strings. Array sits in a file and was created manually. It has close to a 1000 entries with potentially many duplicate keys.
How can I find out which keys are duplicates?
The result I want to get is a list of the keys that have duplicates:
key1, key2, ...
so I can go in the file and fix those keys to make them unique. Any format is fine, just so I know the names of those keys.
key3
has no duplicate so it's fine.
Any help would be greatly appreciated.
Thanks.
Most of you seem to be telling me the same thing :) I know I can't have multiple keys. The problem is that this is a dictionary array created manually in a flat file. The person who created it added the same keys multiple times. The problem is that key1
on one page has translation A
but on other page it should have translation B
, but since both values have the same key in my dictionary array they both display the same value.
I assume this is a one-time job in order to clean out some input data in a file and not something you that needs to happen automatically.
If your data originally is in a CSV type file and you have access to some GNU tools, I often use something like
$ cat filenamv.csv | cut -d, -f1 | sort | uniq -d
This should the first column of the CSV file and print any duplicate keys.
You probalby want to read up on the individual commands (example man uniq
) for just the correct parameters to use in your case.
You can't. Those "duplicate keys" are collapsed as soon as the array is defined. The corresponding values are lost.
array_count_values() might be of some help here :
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
http://www.php.net/manual/en/function.array-count-values.php
array_unique() does a nice job too, if you're satisfied with just deleting duplicate values.
I think, that "Niet the Dark Absol" has right. Anyway, it should look like this to remove duplicate elementes.
$foo2 = array();
foreach($foo as $key => $item)
{
if (!array_key_exists($key, $foo2))
{
$foo2[$key] = $item;
}
}
You can't do this if PHP evaluates the file, but you may try to parse the file manually.
$existing_keys = array();
while (!feof($fh))
{
$str = fgets($fh);
list($key) = explode("=>", $str, 2);
$key = trim($key);
if (isset($existing_keys[$key]))
{
echo "Duplicate key $key
";
} else
{
$existing_keys[$key] = 1;
}
}
Whenever you create an array like this, your first key 'key1' => 'value 1'
will be replaced by 'key1' => 'value 2'
and goes on like that. So you will always have unique keys and last value will be retained.
So you will have to figure out a way to eliminate duplicate keys before storing them into an array.
You could probably load them into MySql, fire some queries. If you want to do this way let me know, I will help you out with the queries.