开发者

Counting distinct values in a multidimensional array

开发者 https://www.devze.com 2023-01-19 17:05 出处:网络
I have an array that looks like the one below.I\'m trying to group and count them, but haven\'t been able to get it to work.

I have an array that looks like the one below. I'm trying to group and count them, but haven't been able to get it to work.

The original $result array looks like this:

Array
(
    [sku] => Array
        (
            [0] => 344
            [1] => 344
            [2] => 164
        )

    [cpk] => Array
        (
            [0] => d456开发者_StackOverflow社区
            [1] => d456
        )
)

I'm trying to take this and create a new array:

$item[sku][344] = 2;
$item[sku][164] = 1;
$item[cpk][d456] = 1;

I've gone through various iterations of in_array statements inside for loops, but still haven't been able to get it working. Can anyone help?


I wouldn't use in_array() personally here.

This just loops through creating the array as it goes.

It seems to work without needing to first set the index as 0.

$newArray = array();

foreach($result as $key => $group) {   
    foreach($group as $member) {
        $newArray[$key][$member]++;
    }    
}
0

精彩评论

暂无评论...
验证码 换一张
取 消