2

How can we find the count of duplicate elements in a multidimensional array and concat of ids ?

I have an array of skill names with feed ids. I need to count skill name and concat of feed ids.

Array
(
    [0] => Array
        (
            [skill_name] => PHP
            [feed_id] => 100
        )
    [1] => Array
        (
            [skill_name] => CSS
            [feed_id] => 105
        )
    [2] => Array
        (
            [skill_name] => Php
            [feed_id] => 110
        )
    [3] => Array
        (
            [skill_name] => Php
            [feed_id] => 111
        )
    [4] => Array
        (
            [skill_name] => CSS
            [feed_id] => 112
        )
    [5] => Array
        (
            [skill_name] => Javascript
            [feed_id] =>114
        )
}

Output should be like below.

Array
(
    [0] => Array
        (
            [skill_name] => PHP
            [feed_id] => 100, 110, 111
            [count]=>3
        )
    [1] => Array
        (
            [skill_name] => CSS
            [feed_id] => 105, 112
            [count]=>2
        )
    [2] => Array
        (
            [skill_name] => Javascript
            [feed_id] => 114
            [count]=>1
        )

}

Thanks in advance!!

2 Answers 2

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

2 Comments

@ Eugen, skill_name should be case-insensitive. In your code Php and PHP returns duplicates.
Well, add some strtoupper() to the first loop!
0

This is perfectly achievable with a single loop using temporary keys and simple concatenation and incrementation.

Code: (Demo)

$array = [
    ['skill_name' => 'PHP', 'feed_id' => 100],
    ['skill_name' => 'CSS', 'feed_id' => 105],
    ['skill_name' => 'Php', 'feed_id' => 110],
    ['skill_name' => 'Php', 'feed_id' => 111],
    ['skill_name' => 'CSS', 'feed_id' => 112],
    ['skill_name' => 'Javascript', 'feed_id' => 114]
];

foreach ($array as $row) {
    $upperSkill = strtoupper($row['skill_name']);
    if (!isset($result[$upperSkill])) {
        $result[$upperSkill] = $row + ['count' => 1];  // plus acts as array_merge here
    } else {
        $result[$upperSkill]['feed_id'] .= ",{$row['feed_id']}"; // concatenate
        ++$result[$upperSkill]['count'];  // increment
    }
}
var_export(array_values($result));  // reindex and display

Output:

array (
  0 => 
  array (
    'skill_name' => 'PHP',
    'feed_id' => '100,110,111',
    'count' => 3,
  ),
  1 => 
  array (
    'skill_name' => 'CSS',
    'feed_id' => '105,112',
    'count' => 2,
  ),
  2 => 
  array (
    'skill_name' => 'Javascript',
    'feed_id' => 114,
    'count' => 1,
  ),
)

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.