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\",
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
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;
}
Another example:
$array = array(
'a',
'a',
'b',
'b',
'b'
);
echo '<br/>Array: ';
print_r($array);
// Unique values
$unique = array_unique($array);
echo '<br/>Unique Values: ';
print_r($unique);
// Duplicates
$duplicates = array_diff_assoc($array, $unique);
echo '<br/>Duplicates: ';
print_r($duplicates);
// Get duplicate keys
$duplicate_values = array_values(array_intersect($array, $duplicates));
echo '<br/>duplicate values: ';
print_r($duplicate_values);
Output:
Array :Array ( [0] => a [1] => a [2] => b [3] => b [4] => b )
Unique Values :Array ( [0] => a [2] => b )
Duplicates :Array ( [1] => a [3] => b [4] => b )
Duplicate Values :Array ( [0] => a [1] => a [2] => b [3] => b [4] => b )