Grupowanie tablic w PHP
Mam tablicę 200 pozycji. Chciałbym wypisać tablicę, ale pogrupować elementy o wspólnej wartości. Podobne do grupy SQL metodą. Powinno to być stosunkowo łatwe do zrobienia, ale potrzebuję również liczenia dla pozycji grupowych.
Czy ktoś ma na to skuteczny sposób? Stanie się to przy każdym załadowaniu strony, więc potrzebuję, aby była szybka i skalowalna.
Czy Mogę wstępnie wrzucić wyniki do czegoś takiego jak Lucene lub sqlite, a następnie uruchomić zapytanie na tym dokumencie na każdej stronie ładować?
Wszelkie myśli byłyby bardzo mile widziane.
5 answers
Po prostu iteruj tablicę i użyj innej tablicy dla grup. Powinien być wystarczająco szybki i prawdopodobnie szybszy niż narzut związany z używaniem sqlite lub podobnego.
$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;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-06-11 17:07:52
$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";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-06-11 17:07:33
Oto krótki przykład:
$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 )
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-06-11 18:30:08
$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);
Wynik:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-14 13:05:14
"$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);
... czy coś mnie ominęło?
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-28 23:12:46