简体   繁体   English

php数组中的新数组中的值相同

[英]same values from php array in new array

i have a problem with arrays, there are not my friends :) 我对数组有问题,没有我的朋友:)

i have a this array: 我有一个这个数组:

Array
(
    [0] => 2012163115
    [1] => 2012163115
    [2] => 2012161817
    [3] => 201214321971
    [4] => 201214321971
    [5] => 201214321971
)

and i need this with all the variables appear more than once 我需要所有变量不止一次出现

Array
(
    [0] => 2012163115
    [1] => 201214321971
)

i try this 我尝试这个

foreach ($array as $val) {
                    if (!in_array($val, $array_temp)) {
                        $array_temp[] = $val;
                    } else {
                        array_push($duplis, $val);
                    }
                }

but the result is 但结果是

Array
(
    [0] => 2012163115
    [1] => 201214321971
    [2] => 201214321971
)

where is my mistake? 我的错误在哪里? thanks for help! 感谢帮助!

array_unique() is there for you. array_unique()为您服务。

EDIT: ops I didn't notice the "more than once" clause, in that case: 编辑:操作,在这种情况下,我没有注意到“多次”子句:

$yourArray = array('a', 'a', 'b', 'c');

$occurrences = array();

array_walk($yourArray, function($item) use(&$occurrences){

    $occurrences[$item]++;

});


$filtered = array();

foreach($occurrences as $key => $value){

    $value > 1 && $filtered[] = $key;

}

var_dump($filtered);
$array = array(
  '2012163115',
  '2012163115',
  '2012161817',
  '201214321971',
  '201214321971',
  '201214321971',
);

$duplication = array_count_values($array);
$duplicates = array();
array_walk($duplication, function($key, $value) use (&$duplicates){
  if ($key > 1)
    $duplicates[] = $value;
});
var_dump($duplicates);

Please see http://php.net/manual/en/function.array-unique.php#81513 请参阅http://php.net/manual/zh/function.array-unique.php#81513

These are added characters to make SO accept the post! 这些是添加的字符,以便SO接受该帖子!

$array_counting = array();
foreach ($array as $val)
    if ( ! in_array($val, $array_counting))
    {
        $array_counting[$val] ++; // counting  
    }

$array_dups = array();
foreach ($array_counting as $key => $count)
{
    if ($count > 1)
        $array_dups[] = $key; // this is more than once
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM