I'm nidhin
I'm nidhin

Reputation: 2662

count of duplicate elements in an array in php

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

Upvotes: 13

Views: 26999

Answers (8)

shishir mishra
shishir mishra

Reputation: 457

Following code will count duplicate element of an array.Please review it and try this code

 $arrayChars=array("green","red","yellow","green","red","yellow","green");

    $arrLength=count($arrayChars);
    $elementCount=array();
  for($i=0;$i<$arrLength-1;$i++)
    {
       $key=$arrayChars[$i];
      if($elementCount[$key]>=1)
       {
          $elementCount[$key]++;
       } else  {
          $elementCount[$key]=1;
       }


   }

   echo "<pre>";
   print_r($elementCount);

OUTPUT:
Array ( [green] => 3 [red] => 2 [yellow] => 2 )

You can also view similar questions with array handling on following link http://solvemyquest.com/count-duplicant-element-array-php-without-using-built-function/

Upvotes: 2

Ja͢ck
Ja͢ck

Reputation: 173562

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

Upvotes: 26

Anand Pal
Anand Pal

Reputation: 11

Check with in_array() function.

Upvotes: 0

Lao
Lao

Reputation: 168

Try this code :

$array_count = array();
foreach ($array as $arr) :
    if (in_array($arr, $array_count)) {
        foreach ($array_count as $key => $count) :
            if ($key == $arr) {
                $array_count[$key]++;
                break;
            }
        endforeach;
    } else {
        $array_count[$arr] = 1;
    }
endforeach;

Upvotes: 0

rOcKiNg RhO
rOcKiNg RhO

Reputation: 631

$orders = array(
 array(
    'lid' => '',
    'lname' => '',

  ))....
$foundIds = array();
foreach ( $orders as $index => $order )
{
  if ( isset( $foundIds[$order['lid']] ) )
  {
    $orders[$index]['is_dupe'] = true;
    $orders[$foundIds[$order['lid']]]['is_dupe'] = true;
  } else {
    $orders[$index]['is_dupe'] = false;
  }
  $foundIds[$order['lid']] = $index;
}

Upvotes: 0

Cedrun
Cedrun

Reputation: 467

foreach ($array as $value) 
{
    $numbers[$value[lid]]++;
}
foreach ($numbers as $key => $value) 
{
    echo 'numbers of '.$key.' equal '.$value.'<br/>';
}

Upvotes: 2

Arun
Arun

Reputation: 68

$array = array('192', '202', '192', '202');
print_r(array_count_values($array));

Upvotes: 0

LeonardChallis
LeonardChallis

Reputation: 7783

The following code will get the counts for all of them - anything > 1 at the end will be repeated.

<?php
$lidCount = array();
$lnameCount = array();

foreach ($yourArray as $arr) {

  if (isset($lidCount[$arr['lid']])) {
    $lidCount[$arr['lid']]++;
  } else {
    $lidCount[$arr['lid']] = 1;
  }

  if (isset($lnameCount [$arr['lname']])) {
    $lnameCount [$arr['lname']]++;
  } else {
    $lnameCount [$arr['lname']] = 1;
  }

}

Upvotes: 0

Related Questions