EagerWolf Posted March 22, 2007 Share Posted March 22, 2007 I have an array. I need to check if key (key => value) is being duplicated... I've already checked all values not to be duplicated and found out they are.. I am trying to make script which would print me which key is being duplicated. Thanks for help! Link to comment https://forums.phpfreaks.com/topic/43864-duplicated-entries-in-array/ Share on other sites More sharing options...
Orio Posted March 22, 2007 Share Posted March 22, 2007 I don't really understand what you want. You can use array_keys() with the search parameter to find all of the keys that have the same value. Orio. Link to comment https://forums.phpfreaks.com/topic/43864-duplicated-entries-in-array/#findComment-212932 Share on other sites More sharing options...
Barand Posted March 22, 2007 Share Posted March 22, 2007 You can't duplicate keys <?php $a = array ( 'a' => 1, 'b' => 2, 'c' => 3, 'b' => 4, // overwrites earlier key 'd' => 5 ); echo '<pre>', print_r($a, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/43864-duplicated-entries-in-array/#findComment-213021 Share on other sites More sharing options...
EagerWolf Posted March 25, 2007 Author Share Posted March 25, 2007 I have got very long file ... and I know that duplicated key rewrites earlier... That is what I want to prevent with script I am trying to do .. Link to comment https://forums.phpfreaks.com/topic/43864-duplicated-entries-in-array/#findComment-214794 Share on other sites More sharing options...
Barand Posted March 25, 2007 Share Posted March 25, 2007 Before adding to the array check if key value is there already <?php if (isset($array[$key])) { echo "Duplicate : $key <br>"; } else { $array[$key] = $someValue; } ?> Link to comment https://forums.phpfreaks.com/topic/43864-duplicated-entries-in-array/#findComment-214827 Share on other sites More sharing options...
EagerWolf Posted March 26, 2007 Author Share Posted March 26, 2007 I've used this method already, but in my case I got array which has about 1000 entries ... and I should check if any key is duplicated.. Thanks for idea anyway... Link to comment https://forums.phpfreaks.com/topic/43864-duplicated-entries-in-array/#findComment-215246 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.