I have a array like this:
$array = array(0 => 'blue', 1 => 'yellow', 2 => 'green', 3 => 'red', 4 => 'red');
and I want to display all the array keys where the values are similars.
Red - 3, 4
Thanks!
I have a array like this:
$array = array(0 => 'blue', 1 => 'yellow', 2 => 'green', 3 => 'red', 4 => 'red');
and I want to display all the array keys where the values are similars.
Red - 3, 4
Thanks!
You can use the below code
$array = array(0 => 'blue', 1 => 'yellow', 2 => 'green', 3 => 'red', 4 => 'red');
// temp array to store unique values
$unique_values = array();
// temp array to store duplicate values
$dup_values = array();
// looping through each value in array
foreach($array as $key=>$value)
{
// If the value is not in unique value i am addig it, if it is then its duplicate so i am adding the keys of duplicate value in $dup_values array
if(!in_array($value, $unique_values))
{
$unique_values[] = $value;
}
else
{
$dup_values[$value] = array_keys($array, $value);
}
}
// displaying $dup_values
var_dump($dup_values);