views:

29

answers:

1

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
            [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?

+2  A: 

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]++;
    }    
}
alex