0

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.

5
  • 3
    Array keys must be unique. $foo array is not what you think. It only has three values. See demo. Commented Dec 23, 2013 at 13:37
  • Did you print the array to confirm what you suspect? I think your keys are not what you think they are. Spaces? Commented Dec 23, 2013 at 13:37
  • If you want to catch a duplicate key before an element is lost you'll have to work with ArrayObject and overload offsetSet() Commented Dec 23, 2013 at 13:38
  • @AmalMurali I know the keys have to be unique. That's my problem. I did say this array is created manually. The person who created it made a mistake and added multiple entries for the same keys :( I need to find out those keys. Commented Dec 23, 2013 at 13:45
  • @6bytes: See andrel's suggestion below. Commented Dec 23, 2013 at 13:46

6 Answers 6

3

You can't. Those "duplicate keys" are collapsed as soon as the array is defined. The corresponding values are lost.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Yes this is a one time job. The file is in a format of an array but I'll try your approach. I'll just do a search and replace for "=>" to make the file more CSV friendly.
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.

Comments

0

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.

Comments

0

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; } } 

Comments

0

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\n"; } else { $existing_keys[$key] = 1; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.