0

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!

4 Answers 4

2
$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);
Sign up to request clarification or add additional context in comments.

Comments

2

array_unique() is there for you.

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);

2 Comments

More than once. The output array should contain two items, not three.
but how? when i use array_unique() i have all values one time, but i need the values there are more than one time in a new array.
0

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

These are added characters to make SO accept the post!

Comments

0
$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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.