-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.php
executable file
·106 lines (75 loc) · 3.34 KB
/
Example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Exampple for grouping arrays
*/
namespace ArrayGrouper;
use ArrayGrouper\Grouper\Collection;
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once __DIR__ . '/vendor/autoload.php';
$array = array(
array('title' => 'The grand budapest hotel', 'director' => 'Wes Anderson', 'year' => '2014', 'rating' => 4.0),
array('title' => 'Easy Rider', 'director' => 'Dennis Hopper', 'year' => '1969', 'rating' => 4.2),
array('title' => 'Coffee & Cigarettes', 'director' => 'Jim Jarmush', 'year' => '2004', 'rating' => 3.7),
array('title' => 'A life aquatic', 'director' => 'Wes Anderson', 'year' => '2014', 'rating' => 4.1),
array('title' => 'The royal tennenbaums', 'director' => 'Wes Anderson', 'year' => '2001', 'rating' => 3.7),
array('title' => 'The grand budapest hotel', 'director' => 'Wes Anderson', 'year' => '2014', 'rating' => 4.1),
array('title' => 'A really bad movie', 'director' => 'Max Maxxen', 'year' => '2014', 'rating' => 0.1),
);
shuffle($array);
/** custom grouping */
$coll = new Collection($array);
$coll->registerGroupingFunction('century', function($arr) {
return (int) ucfirst($arr['year'] / 100 ) + 1 . ' Jh.';
});
$coll->groupByDescending('centuryGroup', array('century'))
->groupBy('anotherKey', array('year','title'));
$groups = $coll->apply();
foreach($groups->getChildren() as $child) {
echo $child->formatCaption('<h1>%century%</h1>'); // use it in caption
foreach ($child->getChildren() as $node) {
echo $node->formatCaption('%year% - %title% (%century%)<br />');
}
}
echo "<hr />";
$coll = new Collection($array);
$coll->groupBy('key', array('year', 'title'));
$groups = $coll->apply();
/** @var $child \ArrayGrouper\Grouper\Group */
foreach($groups->getChildren() as $child) {
echo $child->formatCaption('Im Jahr %year% entstand %title% <br />');
}
$coll = new Collection($array);
$coll->groupBy('aKey', array('year'))
->groupBy('anotherKey', array('title'));
$groups = $coll->apply();
/** @var $child \ArrayGrouper\Grouper\Group */
foreach($groups->getChildren() as $child) {
echo $child->formatCaption('<h1>%year%</h1>');
foreach ($child->getChildren() as $node) {
echo $node->getValue('title') . $node->getValue('rating') . '---' .$node->count() . '<br />';
}
echo $child->formatCaption('Im Jahr %year% entstand %title% <br />');
}
$coll = new Collection($array);
$coll->groupBy('aKey', array('director'))
->groupBy('anotherkey', array('year', 'title'))
->orderByDescending('key', array('rating'));
$groups = $coll->apply();
/** @var $child \ArrayGrouper\Grouper\Group */
foreach($groups->getChildren() as $child) {
echo $child->formatCaption('<h1>%director%</h1>');
foreach ($child->getChildren() as $node) {
echo $node->formatCaption('%title% - %year% - %rating%') . '<br />';
}
}
$coll = new Collection($array);
$coll->groupByDescending('aKey', array('year'))
->orderByDescending('sortKey', array('rating'));
$groups = $coll->apply();
foreach($groups->getChildren() as $child) {
echo $child->formatCaption('<h1>%year%</h1>'); // use it in caption
foreach ($child->getChildren() as $arr) {
echo $child->formatCaption('<h3>%rating%: %title%</h3>', $arr); // format the raw data
}
}