Check if the array contain multiple same values in php [closed]
I have a array like below
$array1 = Array
(
[0] => 20225,2017-02-20
[1] => 20225,2017-02-20
[2] => 10027,2017-02-20
[3] => 10027,2017-02-20
[4] => 10021,2017-02-20
[5] => 20205,2017-02-20
[6] => 50003,2017-02-20
[7] => 20225,2017-02-20
[8] => 20205,2017-02-20
[9] => 10021,2017-02-20
[10] => 50003,2017-02-20
[11] => 10027,2017-02-20
[12] => 20225,2017-02-20
[13] => 20225,2017-02-21
[14] => 20225,2017-02-21
)
if the array contain same result more than 2, I want to get one of those values to another array called $array2
.
Please help me to do this
As You said:- if the array contain same result more than 2, I want to get one of those values to another array called $array2.
You can do it like below:-
$array_new = array_count_values($array1);
$array2 = array();
foreach($array_new as $key=>$val){
if($val >1){ //or do $val >2 based on your desire
$array2[] = $key;
}
}
print_r($array2);
Output:- https://eval.in/834306 OR https://eval.in/834402
If you want to check that array contains duplicates or not then you can do like this:-
if(count(array_unique($array1)) < count($array1)){
echo "Array have some duplicates";
}else{
echo "Array have unique elements";
}
Output:-https://eval.in/834312
If you want to get only unique array then you can do like below:-
$array1 = array_values (array_unique($array1));
Output:- https://eval.in/834317
You can use this simple function to check if your array has duplicates:
function array_has_dupes($array) {
return count($array) !== count(array_unique($array));
}
Here is the usage:
<?php
$array = Array
(
'0' => '20225,2017-02-20',
'1' => '20225,2017-02-20',
'2' => '10027,2017-02-20',
'3' => '10027,2017-02-20',
'4' => '10021,2017-02-20',
'5' => '20205,2017-02-20',
'6' => '50003,2017-02-20',
'7' => '20225,2017-02-20',
'8' => '20205,2017-02-20',
'9' => '10021,2017-02-20',
'10' => '50003,2017-02-20',
'11' => '10027,2017-02-20',
'12' => '20225,2017-02-20',
'13' => '20225,2017-02-21',
'14' => '20225,2017-02-21'
);
$arr_unique = array_unique($array);
$check = count($array) !== count($arr_unique);
$message = 'No duplicates found.';
$arr_duplicates = [];
if($check == 1) {
$message = "Duplicates found.";
$arr_duplicates = array_diff_assoc($array, $arr_unique);
}
echo $message;
print_r($arr_duplicates);
?>
Btw, array_unique()
is what you are looking for.
$array1 = [
[20225,'2017-02-20'],
[20225,'2017-02-20'],
[10027,'2017-02-20'],
[10027,'2017-02-20'],
[10021,'2017-02-20'],
[20205,'2017-02-20'],
[50003,'2017-02-20'],
[20225,'2017-02-20'],
[20205,'2017-02-20'],
[10021,'2017-02-20'],
[50003,'2017-02-20'],
[10027,'2017-02-20'],
[20225,'2017-02-20'],
[20225,'2017-02-21'],
[20225,'2017-02-21']
];
$array2 = array_map("unserialize", array_unique(array_map("serialize", $array1)));
echo '<pre>';
print_r(($array2));`
it's result will be like
Array
(
[0] => Array
(
[0] => 20225
[1] => 2017-02-20
)
[2] => Array
(
[0] => 10027
[1] => 2017-02-20
)
[4] => Array
(
[0] => 10021
[1] => 2017-02-20
)
[5] => Array
(
[0] => 20205
[1] => 2017-02-20
)
[6] => Array
(
[0] => 50003
[1] => 2017-02-20
)
[13] => Array
(
[0] => 20225
[1] => 2017-02-21
)
)