0

I have an array like following and I need to detect all the duplicate keys, not values.

$array1 = array( "a" => "Mike", "b" => "Charles", "b" => "Robert", "c" => "Joseph" ); 

All the functions I've used are focused on values, and If I apply flip_array(), it's automatically deleting the duplicate keys.

2
  • 8
    You can't! If you define it like that the duplicate keys just get overwritten! Commented Jun 1, 2016 at 12:05
  • Edited my answer in order to provide a working solution Commented Jun 1, 2016 at 12:18

2 Answers 2

11

Array-Keys are never duplicates, as they're unique identifiers. (Like Database primary keys)

Declaring $array['b'] twice will cause overriding of the first value.

If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.

Following your logic print_r($array1['b']) would output 2 values, which is impossible.


If you want mulpile values for a key add a dimension:

$array1 = array( "a" => "Mike", "b" => array(1 => "Charles", 2 => "Robert"), "c" => "Joseph" ); 

print_r($array1['b']);

will return

Array ( [1] => Charles [2] => Robert )


EDIT

If there's no way around you have to use regular expressions with preg_match and your array as string:

$array1 =' array( "a" => "Mike", "b" => "Charles", "b" => "Robert", "c" => "Joseph" )'; preg_match_all('/([A-Z])\w+/', $array1, $matches); print_r($matches[0]); 

will return

 Array ( [0] => Mike [1] => Charles [2] => Robert [3] => Joseph ) 
Sign up to request clarification or add additional context in comments.

5 Comments

My porpuse is to correct a huge array I entried manually, used to translate strings with gettext. Now I may have some key repeated.
Then you have to change the keys manually, as they're overwritten if you do something with it. Otherwise write a regex Function and provide the Array as string and create a new key index for the array (0-xxx). But this issue isn't in the scope of the question. Hope this helped you still.
Thanks! I will convert the array to string, and somehow look for string coincidences, I'll post it then.
It returns me preg_match_all() expects parameter 2 to be string, array given.
Did you add the single quotes? Or did you use $array1 twice?
3

Using the answer provided by @TechTreeDev, this is the function I made to show the duplicate keys and values from array. Working demo at IDEOne.

The parameter textOfArray() will be the array's content on simple quotes.

function findCoincidences($textOfArray) { $output = ""; // Locate all the duplicated Strings (keys and values) preg_match_all('/".*?"/', $textOfArray, $matches); // Make array where key = string, and value = repetitions $arrayCoinc = array_count_values($matches[0]); $output = "==== COINCIDENCES ====<br>"; foreach ($arrayCoinc as $k => $v){ if ($v > 1){ $output .= "<b>".$k."</b> Found:".$v."<br>"; } } return $output; } echo findCoincidences($array1); 

Result:

==== COINCIDENCES ==== "a" Found:4 "f" Found:3 "e" Found:3 

1 Comment

Great answer, this should be the accepted one! I tested it out at a demo sandbox and added the sandbox link to your answer, hope this helps someone.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.