使用Count对PHP数组进行分组

时间:2015-07-06 17:43:26

标签: php arrays

我有这个数据样本,我想要做的是将它们组合在一起并计算它们的出现,以便它形成一个新的数组

是否有一个PHP函数来计算这个出现次数,注意到有重复的“值”,我只想要一个新的数组,用它的计数器显示DISTINCT itemName(下面的数组中有多少个)

array("itemName" => $itemName,
"occurence" => $occurenceCount)


Array
(
    [0] => Baked halibut with diced tomatoes##bundle:10##gender:M##category:L
    [1] => Broiled nutty herb crusted salmon##bundle:10##gender:M##category:L
    [2] => Grilled n salted paprika chicken##bundle:10##gender:M##category:L
    [3] => Grilled n salted paprika chicken##bundle:10##gender:M##category:L
    [4] => Grilled n salted paprika chicken##bundle:10##gender:M##category:L
    [5] => Homemade Spanish beef balls##bundle:10##gender:M##category:L
    [6] => Homemade Spanish beef balls##bundle:10##gender:M##category:L
    [7] => Japanese chicken yakitori##bundle:10##gender:M##category:L
    [8] => Slow-cooked dory in bay leaves and thyme##bundle:10##gender:M##category:L
    [9] => Slow-cooked dory in bay leaves and thyme##bundle:10##gender:M##category:L
    [10] => Broiled nutty herb crusted salmon##bundle:10##gender:F##category:L
    [11] => Broiled nutty herb crusted salmon##bundle:10##gender:F##category:L
    [12] => Chilli con carne with minced beef##bundle:10##gender:F##category:L
    [13] => Chilli con carne with minced beef##bundle:10##gender:F##category:L
    [14] => Chilli con carne with minced beef##bundle:10##gender:F##category:L
    [15] => Homemade Spanish beef balls##bundle:10##gender:F##category:L
    [16] => Homemade Spanish beef balls##bundle:10##gender:F##category:L
    [17] => Yoghurt baked tilapia with bell peppers##bundle:10##gender:F##category:L
    [18] => Baked halibut with diced tomatoes##bundle:10##gender:F##category:L
    [19] => Japanese chicken yakitori##bundle:10##gender:F##category:L
    [20] => Broiled nutty herb crusted salmon##bundle:10##gender:M##category:L
    [21] => Broiled nutty herb crusted salmon##bundle:10##gender:M##category:L
    [22] => Chilli con carne with minced beef##bundle:10##gender:M##category:L
    [23] => Chilli con carne with minced beef##bundle:10##gender:M##category:L
    [24] => Homemade Spanish beef balls##bundle:10##gender:M##category:L
    [25] => Homemade Spanish beef balls##bundle:10##gender:M##category:L
    [26] => Japanese chicken yakitori##bundle:10##gender:M##category:L
    [27] => Yoghurt baked tilapia with bell peppers##bundle:10##gender:M##category:L
    [28] => Yoghurt baked tilapia with bell peppers##bundle:10##gender:M##category:L
)

2 个答案:

答案 0 :(得分:1)

使用array_count_values()

$result = array_count_values($array);

收率:

Array
(
    [Baked halibut with diced tomatoes##bundle:10##gender:M##category:L] => 1
    [Broiled nutty herb crusted salmon##bundle:10##gender:M##category:L] => 3
    [Grilled n salted paprika chicken##bundle:10##gender:M##category:L] => 3
    [Homemade Spanish beef balls##bundle:10##gender:M##category:L] => 4
    [Japanese chicken yakitori##bundle:10##gender:M##category:L] => 2
    [Slow-cooked dory in bay leaves and thyme##bundle:10##gender:M##category:L] => 2
    [Broiled nutty herb crusted salmon##bundle:10##gender:F##category:L] => 2
    [Chilli con carne with minced beef##bundle:10##gender:F##category:L] => 3
    [Homemade Spanish beef balls##bundle:10##gender:F##category:L] => 2
    [Yoghurt baked tilapia with bell peppers##bundle:10##gender:F##category:L] => 1
    [Baked halibut with diced tomatoes##bundle:10##gender:F##category:L] => 1
    [Japanese chicken yakitori##bundle:10##gender:F##category:L] => 1
    [Chilli con carne with minced beef##bundle:10##gender:M##category:L] => 2
    [Yoghurt baked tilapia with bell peppers##bundle:10##gender:M##category:L] => 2
)

答案 1 :(得分:0)

// $data contains your array

$counts = array();
foreach ($data as $key => $value) {
    list($title, $bundle, $gender, $category) = explode('##', $value);
    if (isset($counts[$title])):
        ++$counts[$title];
        continue;
    endif;
    $counts[$title] = 1;
}

print_r($counts);

转储$counts

Array
(
    [Baked halibut with diced tomatoes] => 2
    [Broiled nutty herb crusted salmon] => 5
    [Grilled n salted paprika chicken] => 3
    [Homemade Spanish beef balls] => 6
    [Japanese chicken yakitori] => 3
    [Slow-cooked dory in bay leaves and thyme] => 2
    [Chilli con carne with minced beef] => 5
    [Yoghurt baked tilapia with bell peppers] => 3
)