This will remove duplicate items from your array using array_unique():

$new_arr = array_unique($arr, SORT_REGULAR);
Answer from user142162 on Stack Overflow
🌐
PHP
php.net › manual › en › function.array-count-values.php
PHP: array_count_values - Manual
array_unique() - Removes duplicate values from an array · array_values() - Return all the values of an array · count_chars() - Return information about characters used in a string · Learn How To Improve This Page • Submit a Pull Request ...
🌐
Stack Overflow
stackoverflow.com › questions › 13616404 › counting-duplicate-values-in-a-multidimensional-array-and-populating-values-base
php - Counting duplicate values in a multidimensional array and populating values based on that - Stack Overflow
If so, you could: function multi_counter($big_array, $sorted, $storage){ $multi_count=0; // since by default there are no multiples $multi_items=array(0); // starting at the beginning of the array $length = count($big_array); // counting before ...
🌐
Quora
quora.com › How-do-I-count-duplicate-values-in-an-array-in-PHP
How to count duplicate values in an array in PHP - Quora
Answer (1 of 5): I’ll skip the “in PHP” part as I only barely know it… There’s a bunch of ways to count duplicates in an array, with varying levels of performance and space usage: * If the range of values in the array are small enough, you can make a tracking array that is initialized ...
🌐
Stack Overflow
stackoverflow.com › questions › 41408605 › how-do-i-count-same-values-in-an-multidimensional-array › 41408634
php - How do I count same values in an multidimensional array? - Stack Overflow
$list = array ( Array ( 'name' => 'bmw', 'id' => 1 ), Array ( 'name' => 'toyota', 'id' => 1 ),Array ( 'name' => 'tata', 'id' => 2 ),Array ( 'name' => 'bajaj', 'id' => 3 ),Array ( 'name' => 'kawasaki', 'id' => 3 ), Array ( 'name' => 'tvs', 'id' => 3 ),Array ( 'name' => 'mitsubishi', 'id' => 2 ) ); $results = array_count_values(array_column($list, 'id')); ... Sign up to request clarification or add additional context in comments. ... Maybe there is a nicer solution, but this should work. Assuming that your array is saved in $arr.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 54100002 › counting-duplicate-values-in-multidimension-array-of-objects
php - Counting Duplicate Values In Multidimension Array of Objects - Stack Overflow
I have a multidimensional array objects $all2[$i][$j] which is undefined but received through my laravel sql query request: for($i=0; $i<(count($all));$i++){ $uID = HashtagFollow::select('user_id')->where('hashtag_id', $all[$i]->hashtag_id)->where('user_id', '!=', $loggedin->id)->get(); for($j=0;$j<count($uID);$j++){ $all2[$i][$j] = HashtagFollow::select('hashtag_id')->where('user_id', $uID[$j]->user_id)->whereNotIn('hashtag_id', $all)->orderBy('hashtag_id')->get(); } }
Top answer
1 of 4
4

The key problem is that an array(that with an id and a title) is not hashable.

So we can try to hash it, just join them all together, and make the result string as a hash key.

So, we maintain a hash table, which key is the object hash key, and the value is the result array index.

And then, For each item we travel, we can find the result array position by the help of the hash table, and then add the count.

Here's my code.

<?php

$array_in = array(
    array('id'=>1, 'title'=>'BMW'),
    array('id'=>1, 'title'=>'BMW'),
    array('id'=>2, 'title'=>'Mercedes'),
);

$hash = array();
$array_out = array();

foreach($array_in as $item) {
    $hash_key = $item['id'].'|'.$item['title'];
    if(!array_key_exists($hash_key, $hash)) {
        $hash[$hash_key] = sizeof($array_out);
        array_push($array_out, array(
            'id' => $item['id'],
            'title' => $item['title'],
            'count' => 0,
        ));
    }
    $array_out[$hash[$hash_key]]['count'] += 1;
}

var_dump($array_out);
2 of 4
2

In this sample I've used array_reduce to build a second array out of appropriate elements from your first array. Due to the way that I check for duplicate values, this assumes that your initial array is sorted (all the dupes are next to each other).

function dedupe($carry, $item) {
    // check if array is non empty, and the last item matches this item
    if (count($carry) > 0 
        && $carry[count($carry) - 1][0] == $item[0] 
        && $carry[count($carry) - 1][1] == $item[1]) {

        // the array contains a match, increment counter
        $carry[count($carry) - 1][2]++;
    } else {
        // there was no match, add element to array
        $carry[] = array($item[0], $item[1], 1);
    }
    return $carry;
}

$cars = array(
    array(1, "BMW"),
    array(1, "BMW"),
    array(2, "Mercedes")
);

$uniques = array_reduce($cars, "dedupe", array());

var_dump($uniques);
Top answer
1 of 3
1

Solved it without fancy stuff, but it should work:

/**
 * Check if value exists in array
 */
function value_exists($array, $value) {
    $count = 0;
    foreach($array as $v) {
        if($v['Num'] == $value) {
            $count++;
        }
    }
    if ($count > 1) {
        return $count;
    }
}

$duplicated = array();
foreach ($Num as $key => $array ) {
    if($count = value_exists($Num, $array['Num'] )) {
        $duplicated[] = $array['Num'];
    }
}

echo 'Duplicated: ' . count($duplicated);
2 of 3
1

As of php 5.5 (because of array_column*) you can use

<?php
$num = array (
    array ( 'Num' => 2 ), array ( 'Num' => 3 ), array ( 'Num' => 13 ), array ( 'Num' => 2 ),
    array ( 'Num' => 3 ),   array ( 'Num' => 3 ),   array ( 'Num' => 1 ), array ( 'Num' => 44 ) 
);

$x = array_filter(
    array_count_values(
        array_column($num, 'Num')
    ),
    function($e) { return $e>1; }
);
var_export($x);

i.e.
- get all the elements 'Num' in $num as a "flat, 1-d" array.
- count the occurence of each value
- only keep count()>1 in the array

-- * in the user contributes notes for array_column there are fill-ins for php <5.5
And since you only have one element per array in $num you could also use array_map/array_shift

edit: a more verbose version without array_column

<?php
$num = array (
    array ( 'Num' => 2 ), array ( 'Num' => 3 ), array ( 'Num' => 13 ), array ( 'Num' => 2 ),
    array ( 'Num' => 3 ),   array ( 'Num' => 3 ),   array ( 'Num' => 1 ), array ( 'Num' => 44 ) 
);
// first remove the superfluous second dimension
$num = array_map(function($e) { return $e['Num']; }, $num); // -> $num = [ 2,3,13,2,3,3,1,44]
$num = array_count_values($num); // -> $num = [2=>2, 3=>3, 13=>1, 1=>1, 44=>1]
$num = array_filter($num, function($e) { return $e>1; }); // -> $num = [2=>2, 3=>3]

printf('There are %d duplicate values. A total of %d elements are doublets',
    count($num), array_sum($num)
);

prints There are 2 duplicate values. A total of 5 elements are doublets