Php创建具有关联值和计数的数组

Sha*_*nov -1 php arrays

我有一个这种结构的数组

Array
(
    [0] => Array
        (
            [profileId] => 1000000407
            [locationId] => 207

        )

    [1] => Array
        (
            [profileId] => 1000000407
            [locationId] => 250

        )

    [2] => Array
        (
            [profileId] => 1000000398
            [locationId] => 250

        )

    [3] => Array
        (
            [profileId] => 1000000393
            [locationId] => 250

        )
    [4] => Array
            (
             [profileId] => 1000000393
             [locationId] => 250

            )
)
Run Code Online (Sandbox Code Playgroud)

从这个数组我想创建一个新的数组,其中位置Ids为键,并且数组包含与该位置关联的profileId的计数.所以在这种情况下我需要回归

Array
(
   [207] => Array
        (
         [1000000407] => 1
        )
   [250] => Array
        (
         [1000000407] => 1
         [1000000398] => 1
         [1000000393] => 2
        )
)
Run Code Online (Sandbox Code Playgroud)

我很欣赏这可能很简单,但我似乎无法绕过它.

Ete*_*al1 6

我通常array_reduce用于这些任务,如下所示:

$new = array_reduce(
     $old,
     function($result, $item) 
     {
         $result[$item['locationId']][$item['profileId']] += 1;
         return $result;
     }
)
Run Code Online (Sandbox Code Playgroud)