Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, December 27, 2015

Get the keys for duplicate values in an array

Get the keys for duplicate values in an array


if my arrays is

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 )   
  1. Now how can I display the keys with duplicate values? eg. Here the function should NOT return ([0],[13]) since there are no duplicates with the values.
  2. How to find the key for the value , eg."2011-06-29" it should return [15],[16]

Answer by Trey for Get the keys for duplicate values in an array


function get_keys_for_duplicate_values($my_arr, $clean = false) {      if ($clean) {          return array_unique($my_arr);      }        $dups = $new_arr = array();      foreach ($my_arr as $key => $val) {        if (!isset($new_arr[$val])) {           $new_arr[$val] = $key;        } else {          if (isset($dups[$val])) {             $dups[$val][] = $key;          } else {             $dups[$val] = array($key);          }        }      }      return $dups;  }  

obviously the function name is a bit long;)

Now $dups will contain a multidimensional array keyed by the duplicate value, containing each key that was a duplicate, and if you send "true" as your second argument it will return the original array without the duplicate values.

Alternately you could pass the original array as a reference and it would adjust it accordingly while returning your duplicate array

Answer by Dogbert for Get the keys for duplicate values in an array


$array = array(0 => "1", 1 => "1", 2 => "2", 3 => "3");  $count = array();  foreach($array as $key => $value) {    if(!isset($count[$value])) {      $count[$value] = 0;    }    $count[$value]++;  }      $result = array_filter($count, function($value) {    return $value > 1;  });    $result = array_keys($result);    var_dump($result);  

Output

array(1) {    [0]=>    int(1)  }  

Answer by darkylmnx for Get the keys for duplicate values in an array


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

Answer by Francois Deschenes for Get the keys for duplicate values in an array


I'll answer the second question first. You want to use array_keys with the "search_value" specified.

$keys = array_keys($array, "2011-06-29")  

In the example below, $duplicates will contain the duplication values while $result will contain ones that are not duplicates. To get the keys, simply use array_keys.

Result:

// $duplicates  Array  (      [1] => a  )    // $result  Array  (      [2] => b      [3] => c      [4] => d  )    // $unique_keys  Array  (      [0] => 2      [1] => 3      [2] => 4  )  

Answer by Dereleased for Get the keys for duplicate values in an array


$array = array(      '2011-06-21','2011-06-22','2011-06-22','2011-06-23',      '2011-06-23','2011-06-24','2011-06-24','2011-06-25',      '2011-06-25','2011-06-26','2011-06-26','2011-06-27',      '2011-06-27','2011-06-28','2011-06-29','2011-06-29',      '2011-06-30','2011-06-30','2011-07-01','2011-07-01',      '2011-07-02','2011-07-02','2011-07-03','2011-07-03',      '2011-07-04','2011-07-04','2011-07-05','2011-07-05',      '2011-07-06','2011-07-06','2011-07-07','2011-07-07',  );    function getDupKeys(array $array, $return_first = true, $return_by_key = true) {      $seen = array();      $dups = array();        foreach ($array as $k => $v) {          $vk = $return_by_key ? $v : 0;          if (!array_key_exists($v, $seen)) {              $seen[$v] = $k;              continue;          }          if ($return_first && !array_key_exists($v, $dups)) {              $dups[$vk][] = $seen[$v];          }          $dups[$vk][] = $k;      }      return $return_by_key ? $dups : $dups[0];  }  

If both optional parameters are true, it returns an array of arrays; the key of each child array will be the value which was not unique, and the values of the array will be all those keys which had that value.

If the first optional parameter is false, then only keys after the first instance of a non-unique value will be returned (i.e., for the given array, each value returns only one key, the second time it occurred, instead of the first).

If the second parameter is optional, then instead of returning an array of arrays, it returns a flat array containing all duplicate keys (exactly which keys it returns are dictated by the prior optional parameter).

Here's a dumpprint_r, cause it's prettier:

print_r(getDupKeys($array));    Array  (      [2011-06-22] => Array          (              [0] => 1              [1] => 2          )        [2011-06-23] => Array          (              [0] => 3              [1] => 4          )        [2011-06-24] => Array          (              [0] => 5              [1] => 6          )        [2011-06-25] => Array          (              [0] => 7              [1] => 8          )        [2011-06-26] => Array          (              [0] => 9              [1] => 10          )        [2011-06-27] => Array          (              [0] => 11              [1] => 12          )        [2011-06-29] => Array          (              [0] => 14              [1] => 15          )        [2011-06-30] => Array          (              [0] => 16              [1] => 17          )        [2011-07-01] => Array          (              [0] => 18              [1] => 19          )        [2011-07-02] => Array          (              [0] => 20              [1] => 21          )        [2011-07-03] => Array          (              [0] => 22              [1] => 23          )        [2011-07-04] => Array          (              [0] => 24              [1] => 25          )        [2011-07-05] => Array          (              [0] => 26              [1] => 27          )        [2011-07-06] => Array          (              [0] => 28              [1] => 29          )        [2011-07-07] => Array          (              [0] => 30              [1] => 31          )    )  

print_r(getDupKeys($array, false));    Array  (      [2011-06-22] => Array          (              [0] => 2          )        [2011-06-23] => Array          (              [0] => 4          )        [2011-06-24] => Array          (              [0] => 6          )        [2011-06-25] => Array          (              [0] => 8          )        [2011-06-26] => Array          (              [0] => 10          )        [2011-06-27] => Array          (              [0] => 12          )        [2011-06-29] => Array          (              [0] => 15          )        [2011-06-30] => Array          (              [0] => 17          )        [2011-07-01] => Array          (              [0] => 19          )        [2011-07-02] => Array          (              [0] => 21          )        [2011-07-03] => Array          (              [0] => 23          )        [2011-07-04] => Array          (              [0] => 25          )        [2011-07-05] => Array          (              [0] => 27          )        [2011-07-06] => Array          (              [0] => 29          )        [2011-07-07] => Array          (              [0] => 31          )    )  

print_r(getDupKeys($array, true, false));    Array  (      [0] => 1      [1] => 2      [2] => 3      [3] => 4      [4] => 5      [5] => 6      [6] => 7      [7] => 8      [8] => 9      [9] => 10      [10] => 11      [11] => 12      [12] => 14      [13] => 15      [14] => 16      [15] => 17      [16] => 18      [17] => 19      [18] => 20      [19] => 21      [20] => 22      [21] => 23      [22] => 24      [23] => 25      [24] => 26      [25] => 27      [26] => 28      [27] => 29      [28] => 30      [29] => 31  )  

print_r(getDupKeys($array, false, false));    Array  (      [0] => 2      [1] => 4      [2] => 6      [3] => 8      [4] => 10      [5] => 12      [6] => 15      [7] => 17      [8] => 19      [9] => 21      [10] => 23      [11] => 25      [12] => 27      [13] => 29      [14] => 31  )  

Answer by hakre for Get the keys for duplicate values in an array


I really like Francois answer, here is something I came up with that preserves keys. I'll answer the first question first:

$array = array('2011-06-21', '2011-06-22', '2011-06-22');  /**   * flip an array like array_flip but   * preserving multiple keys per an array value   *    * @param array $a   * @return array   */  function array_flip_multiple(array $a) {      $result = array();      foreach($a as $k=>$v)          $result[$v][]=$k          ;      return $result;  }    $hash = array_flip_multiple($array);    // filter $hash based on your specs (2 or more)  $hash = array_filter($hash, function($items) {return count($items) > 1;});    // get all remaining keys  $keys = array_reduce($hash, 'array_merge', array());    var_dump($array, $hash, $keys);  

output is:

# original array  array(3) {    [0]=>    string(10) "2011-06-21"    [1]=>    string(10) "2011-06-22"    [2]=>    string(10) "2011-06-22"  }    # hash (filtered)  array(1) {    ["2011-06-22"]=>    array(2) {      [0]=>      int(1)      [1]=>      int(2)    }  }    # the keys  array(2) {    [0]=>    int(1)    [1]=>    int(2)  }  

So now the second question:

Just use the $hash to obtain the keys for the value:

var_dump($hash['2011-06-22']); returns the keys.

Benefit is, if you need to check multiple values, data is already stored in the hash and available for use.

Answer by Divey for Get the keys for duplicate values in an array


I had a similar problem as question #1 from the OP. All I needed were the keys for duplicate values in my original array. Here's what I came up with:

$array = array('yellow', 'red', 'green', 'brown', 'red', 'brown');    $counts = array_count_values($array);  $filtered = array_filter($counts, function($value) {      return $value != 1;  });  $result = array_keys(array_intersect($array, array_keys($filtered)));  

And for the output:

print_r($result);  Array  (      [0] => 1      [1] => 3      [2] => 4      [3] => 5  )  

Answer by Sandeep Panesar for Get the keys for duplicate values in an array


function getDuplicateValueKeys($my_arr, $clean = false)   {      if ($clean) {          return array_unique($my_arr);      }      $dups = array();      $new_arr = array();      $dup_vals = array();        foreach ($my_arr as $key => $value) {          if (!isset($new_arr[$value])) {              $new_arr[$value] = $key;          } else {              array_push($dup_vals,$value);          }      }        foreach ($my_arr as $key => $value) {          if (in_array($value, $dup_vals)) {              if (!isset($dups[$value])) {                  $dups[$value]=array($key);              }else{                  array_push($dups[$value],$key);              }          }      }        return $dups;  }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.