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! Quote Link to comment 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. Quote Link to comment 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>'; ?> Quote Link to comment 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 .. Quote Link to comment 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; } ?> Quote Link to comment 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.