我有一个如下所示的数组
$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
)
如果数组包含的结果大于2,我希望将其中一个值转换为另一个名为$array2
的数组。
请帮我这样做
答案 0 :(得分:2)
您可以使用此简单函数检查您的数组是否有重复项:
function array_has_dupes($array) {
return count($array) !== count(array_unique($array));
}
以下是用法:
<?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);
?>
顺便说一下,array_unique()
就是你要找的。
答案 1 :(得分:1)
正如您所说: - 如果数组包含的结果大于2,我希望将其中一个值转换为另一个名为$array2.
的数组 < / p>
你可以这样做: -
$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);
输出: - https://eval.in/834306或https://eval.in/834402
如果要检查该数组是否包含重复项,则可以这样做: -
if(count(array_unique($array1)) < count($array1)){
echo "Array have some duplicates";
}else{
echo "Array have unique elements";
}
输出: - https://eval.in/834312
如果你想获得唯一的数组,那么你可以这样做: -
$array1 = array_values (array_unique($array1));
输出: - https://eval.in/834317
答案 2 :(得分:1)
<?php
$array1 = [
"20225,2017-02-20",
"20225,2017-02-20",
"10027,2017-02-20",
"10027,2017-02-20",
"10027,2017-02-20",
"10021,2017-02-20"
];
$array_unique = [];
$array2 = [];
foreach($array1 as $value) {
if(!in_array($value, $array_unique)) {
$array_unique[] = $value;
} else {
$array2[] = $value;
}
}
echo 'Unique Values: ';
print_r($array_unique);
echo '<hr /> Duplicate Values: ';
print_r(array_unique($array2));
签到: - http://phptester.net/
答案 3 :(得分:1)
$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));`
它的结果就像
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
)
)