Asked  7 Months ago    Answers:  5   Viewed   38 times

Hi, How can we find the count of duplicate elements in a multidimensional array ?

I have an array like this

Array
(
    [0] => Array
        (
            [lid] => 192
            [lname] => sdsss
        )

    [1] => Array
        (
            [lid] => 202
            [lname] =>  testing
        )

    [2] => Array
        (
            [lid] => 192
            [lname] => sdsss
        )

    [3] => Array
        (
            [lid] => 202
            [lname] =>  testing
        )

)

How to find the count of each elements ?

i.e, count of entries with id 192,202 etc

 Answers

50

You can adopt this trick; map each item of the array (which is an array itself) to its respective ['lid'] member and then use array_count_value() to do the counting for you.

array_count_values(array_map(function($item) {
    return $item['lid'];
}, $arr);

Plus, it's a one-liner, thus adding to elite hacker status.

Update

Since 5.5 you can shorten it to:

array_count_values(array_column($arr, 'lid'));
Wednesday, March 31, 2021
 
SuperString
answered 7 Months ago
89

You should modify your call for array_unique to have it include the SORT_REGULAR flag.

$arr2 = array_unique($arr, SORT_REGULAR);
Wednesday, March 31, 2021
 
Terry
answered 7 Months ago
68

The actual problem is you're not assigning the result of array_reverse to anything

// change this
array_reverse($respArr);

// to this
$respArr = array_reverse($respArr);
Saturday, May 29, 2021
 
KingCrunch
answered 5 Months ago
67
//Assumption: Input is in $in

//Step 1: Cumulate
$tmp=array();
foreach ($in as $skill) {
  if (isset($tmp[$skill['skill_name']]))
    $tmp[$skill['skill_name']][]=$skill['feed_id'];
  else
    $tmp[$skill['skill_name']]=array($skill['feed_id']);
}

//Step 2: Fix up desired output format
$out=array();
foreach ($tmp as $k=>$v)
  $out[]=array(
    'skill_name' => $k,
    'feed_id' => implode(', ', $v),
    'count' => sizeof($v)
  );

//Result is in $out
Saturday, May 29, 2021
 
freeMagee
answered 5 Months ago
72

array_count_values

array_count_values() returns an array using the values of array as keys and their frequency in array as values.

$varArray = array(); // take a one empty array

foreach ($rowa as $rowsa)
{
    $sql = "SELECT count(*) as NUMBER FROM BANDZENDINGEN WHERE FB_AFGESLOTEN = 'F' AND FB_AKTIEF = 'T' AND FI_AFVOERKANAAL = 1 AND FI_RAYONID = $rowsa  AND FI_VERRIJKINGID < 1;";
    $sfh = $dbh->prepare($sql);
    $sfh->execute();
    $row = $sfh->fetchAll(PDO::FETCH_COLUMN, 0);

    array_push($row, $rows['FC_RAYON']);
    //print_r($row);
    //array_push($varArray,$rows['FC_RAYON']); // Also May I think $rows['FC_RAYON'] is give value like RT-SCB-PB01,ASDC-PBSN etc.
    array_push($varArray,$row[1]); // I have push the send value of array like RT-SCB-PB01,ASDC-PBSN etc. and make in single array.
}

$dupArrays = array_count_values($varArray); // It will return Counts all the values of an array
echo 'Total No Items: '.count($dupArrays).'<br><br>';
echo "<pre>";
print_r($dupArrays);
echo "</pre>";

The output will be :

Total No Items: 3

Array
(
    [RT-SCB-PB01] => 3 // Count of duplicate value of RT-SCB-PB01 is 3
    [ASDC-PBSN] => 4 // Count of duplicate value of ASDC-PBSN is 3
    [ASDW-PBSN] => 1 // Count of duplicate value of ASDW-PBSN is 3
)

Get using foreach method

foreach($dupArrays as $key => $value){
    echo $key.' Count '.$value.' times.';
    echo "<br>";
}

Output:

RT-SCB-PB01 Count 3 times.
ASDC-PBSN Count 4 times.
ASDW-PBSN Count 1 times.
Saturday, May 29, 2021
 
csi
answered 5 Months ago
csi
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :