在 PHP 中对数组进行分组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/982344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:33:47  来源:igfitidea点击:

Grouping arrays in PHP

phparraysalgorithmdata-structuresgrouping

提问by GivP

I have an array of 200 items. I would like to output the array but group the items with a common value. Similar to SQL's GROUP BY method. This should be relatively easy to do but I also need a count for the group items.

我有一个包含 200 个项目的数组。我想输出数组,但用一个共同的值对项目进行分组。类似于 SQL 的 GROUP BY 方法。这应该相对容易做到,但我还需要对组项目进行计数。

Does anyone have an efficient way of doing this? This will happen on every page load so I need it to be fast and scalable.

有没有人有这样做的有效方法?这将在每个页面加载时发生,因此我需要它快速且可扩展。

Could I prehaps dump the results into something like Lucene or sqlite then run a query on that document on each page load?

我可以预先将结果转储到 Lucene 或 sqlite 之类的东西中,然后在每个页面加载时对该文档运行查询吗?

Any thoughts would be greatly appreciated.

任何想法将不胜感激。

回答by Ferdinand Beyer

Just iterate over the array and use another array for the groups. It should be fast enough and is probably faster than the overhead involved when using sqlite or similar.

只需遍历数组并为组使用另一个数组。它应该足够快,并且可能比使用 sqlite 或类似的开销更快。

$groups = array();
foreach ($data as $item) {
    $key = $item['key_to_group'];
    if (!isset($groups[$key])) {
        $groups[$key] = array(
            'items' => array($item),
            'count' => 1,
        );
    } else {
        $groups[$key]['items'][] = $item;
        $groups[$key]['count'] += 1;
    }
}

回答by chaos

$groups = array();
foreach($items as $item)
    $groups[$item['value']][] = $item;
foreach($groups as $value => $items)
    echo 'Group ' . $value . ' has ' . count($items) . ' ' . (count($items) == 1 ? 'item' : 'items') . "\n";

回答by racerror

Here's a quick example:

这是一个快速示例:

$a = array(1, 2, 3, 1, 2, 3, 3, 2, 3, 2, 3, 4, 4, 1);
$n = array_count_values($a);
arsort($n);

print_r($n);

Array ( [3] => 5 [2] => 4 [1] => 3 [4] => 2 )

打印_r($n);

数组( [3] => 5 [2] => 4 [1] => 3 [4] => 2)

回答by Marcos Nakamine

$aA = array_count_values(array(1,2,3,4,5,1,2,3,4,5,6,1,1,1,2,2));
$aB = array();
foreach($aA as $index=>$aux){
     array_push($aB,$index);
}
print_r($aB);

Result:

结果:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) 

回答by SuperDude

"$Switches" Array with [3] elements
0       
    SwitchID    1   
    name    k?  
    type    output  
    displayAs   button  
    value   on  
    groupname   group1  
1   Array [6]   
2   Array [6]   


// this will sort after groupname

$result = array();
$target = count($Switches);
for($i=0;$i<$target;$i++)
{
    $groupname = $Switches[$i]["groupname"];

    $result[$groupname][] = $Switches[$i];
}

// count amount of groups
$groupCount = count($result);

... or did i miss something?

……还是我错过了什么?