Asked  1 Year ago    Answers:  5   Viewed   12 times

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!!

 Answers

5
//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
 
4

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'));
Thursday, April 1, 2021
 
2

I updated your function and it is giving expected output. Please give a try as following.

function array_key_exists_r($needle, $haystack){
    $result = array_key_exists($needle, $haystack);
    if ($result) 
    {
        foreach ($haystack as $a=>$v) 
        {
            if($needle == $a)
                return $haystack[$a];
            if (is_array($v)) {
                $result = array_key_exists_r($needle, $v);
            }
            if ($result) return $result;
        }
    }
    foreach ($haystack as $v) {
        if (is_array($v)) {
            $result = array_key_exists_r($needle, $v);
        }
        if ($result) return $result;
    }
    return $result;
};
Thursday, April 1, 2021
5

I think this might work :P

function cool_function($strs){
    // Black list
    $toExclude = array();

    foreach($strs as $s){
        // If it's not on blacklist, then search for it
        if(!in_array($s, $toExclude)){
            // Explode into blocks
            foreach(explode(" ",$s) as $block){
                // Search the block on array
                $found = preg_grep("/" . preg_quote($block) . "/", $strs);
                foreach($found as $k => $f){
                    if($f != $s){
                        // Place each found item that's different from current item into blacklist
                        $toExclude[$k] = $f;
                    }
                }
            }
        }
    }

    // Unset all keys that was found
    foreach($toExclude as $k => $v){
        unset($strs[$k]);
    }

    // Return the result
    return $strs;
}

$strs = array("Lincoln Crown","Crown Court","go holiday","house fire","John Hinton","Hinton Jailed");
print_r(cool_function($strs));

Dump:

Array
(
    [0] => Lincoln Crown
    [2] => go holiday
    [3] => house fire
    [4] => John Hinton
)
Thursday, April 1, 2021
 
Wookai
 
2

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
 
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 :
 
Share