13

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

2
  • Simply loop in each and create a new array with lid as key and increment (++) as value. Commented Nov 16, 2012 at 9:12
  • 1
    Does it matter what key under the values are? or it count as duplicate if the same value is under various different keys? Commented Nov 16, 2012 at 9:13

8 Answers 8

26

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

1 Comment

"adding to elite hacker status"! LOL! +1
2
foreach ($array as $value) 
{
    $numbers[$value[lid]]++;
}
foreach ($numbers as $key => $value) 
{
    echo 'numbers of '.$key.' equal '.$value.'<br/>';
}

Comments

2

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/

Comments

0

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

}

Comments

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

Comments

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

Comments

0

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;

Comments

0

Check with in_array() function.

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.