scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload |
||
4 | |||
5 | use Fusonic\Linq\Linq; |
||
6 | $files = glob("/tmp/*"); |
||
7 | |||
8 | // Group all files by its filesize. |
||
9 | |||
10 | ### Plain PHP: ### |
||
11 | $data = array(); |
||
12 | foreach($files as $file) { |
||
13 | $currentSize = filesize($file); |
||
14 | $data[] = array("name" => $file, "size" => $currentSize); |
||
15 | } |
||
16 | |||
17 | uasort($data, function($a, $b) { |
||
18 | $as = $a['size']; |
||
19 | $bs = $b['size']; |
||
20 | if($as == $bs) { return 0; } |
||
21 | else return $as < $bs ? 1 : -1; |
||
22 | }); |
||
23 | |||
24 | $grouped = array(); |
||
25 | foreach($data as $x) |
||
26 | { |
||
27 | if(isset($grouped[$x['size']])) { |
||
28 | $grouped[$x['size']][] = $x; |
||
29 | } |
||
30 | else { |
||
31 | $grouped[$x['size']] = array($x); |
||
32 | } |
||
33 | } |
||
34 | |||
35 | foreach($grouped as $key => $value) { |
||
36 | echo $key . " (" . count($value) . ")" . "<br />"; |
||
37 | foreach($value as $file) { |
||
38 | echo " -" . $file["name"] . "<br>"; |
||
39 | } |
||
40 | }; |
||
41 | |||
42 | ### Linq: ### |
||
43 | |||
44 | echo "<br/><br> Linq: <br /><br>"; |
||
45 | |||
46 | $linq = Linq::from($files) |
||
47 | ->select(function($x) { return array("name" => $x, "size" => filesize($x)); }) |
||
48 | ->orderByDescending(function($x) { return $x['size']; }) |
||
49 | ->groupBy(function($x) { return $x['size']; }) |
||
50 | ->orderByDescending(function($x) { return $x->count(); }) |
||
51 | ->each(function($x) { |
||
52 | echo $x->key() . " (" . $x->count() . ")" . "<br />"; |
||
53 | $x->each(function($y) { echo " -" . $y["name"] . "<br>"; }); |
||
54 | }); |