Get the keys for duplicate values in an array

前端 未结 9 1540
温柔的废话
温柔的废话 2020-11-28 11:11

I have the following array:

$myarray = Array(\"2011-06-21\", \"2011-06-22\", \"2011-06-22\", \"2011-06-23\", \"2011-06-23\", \"2011-06-24\", \"2011-06-24\",         


        
9条回答
  •  抹茶落季
    2020-11-28 11:32

    here is a code dude

       $your_array = array(0 => '2011-06-21', 1 => '2011-06-22', 2 => '2011-06-22', 3 => '2011-06-23', 4 =>
    '2011-06-23', 5 => '2011-06-24', 6 => '2011-06-24', 7 => '2011-06-25', 8 => '2011-06-25', 9 
    => '2011-06-26', 10 => '2011-06-26', 11 => '2011-06-27', 12 => '2011-06-27', 13 => '2011-06-  
    28', 14 => '2011-06-29', 15 => '2011-06-29', 16 => '2011-06-30', 17 => '2011-06-30', 18 => 
    '2011-07-01', 19 => '2011-07-01', 20 => '2011-07-02', 21 => '2011-07-02', 22 => '2011-07-03', 
    23 => '2011-07-03', 24 => '2011-07-04', 25 => '2011-07-04', 26 => '2011-07-05', 27 => '2011-
    07-05', 28 => '2011-07-06', 29 => '2011-07-06', 30 => '2011-07-07', 31 => '2011-07-07');
    
    $keys_of_duplicated = array();
    $array_keys = array();
    
    foreach($your_array as $key => $value) {
        //- get the keys of the actual value
        $array_keys = array_keys($your_array, $value);
    
        //- if there is more then one key collected we register it
        if(count($array_keys) > 1) {
            //- foreach key that have the same value we check if i'ts already registered
            foreach($array_keys as $key_registered) {
                //- if not registered we register it
                if(!in_array($key_registered,  $keys_of_duplicated)) {
                     $keys_of_duplicated[] = $key_registered;
                }
            }
        }
    }
    
    var_dump($keys_of_duplicated);
    

    $keys_of_duplicated is now the array that contains the keys of duplicated arrays ;) bye

提交回复
热议问题