1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Sergey Glagolev <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2013 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package frontend.share
|
8
|
|
|
*/
|
9
|
|
|
class Statistics
|
10
|
|
|
{
|
11
|
|
|
protected $lengths = array();
|
12
|
|
|
|
13
|
|
|
protected $elements;
|
14
|
|
|
|
15
|
|
|
/**
|
16
|
|
|
* @param array $elements
|
17
|
|
|
*/
|
18
|
|
|
public function __construct(array $elements)
|
19
|
|
|
{
|
20
|
|
|
foreach($elements as $element)
|
21
|
|
|
$this->lengths[] = count($element);
|
22
|
|
|
|
23
|
|
|
$this->elements = $elements;
|
24
|
|
|
}
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* Получаем возможные сочетания элементов массива
|
28
|
|
|
*
|
29
|
|
|
* @return array
|
30
|
|
|
*/
|
31
|
|
|
public function getCombinations()
|
32
|
|
|
{
|
33
|
|
|
if( empty($this->elements) )
|
34
|
|
|
return array();
|
35
|
|
|
|
36
|
|
|
$combinations = array();
|
37
|
|
|
|
38
|
|
|
foreach($this->getCounter() as $i => $combination)
|
39
|
|
|
{
|
40
|
|
|
foreach($combination as $j => $value)
|
41
|
|
|
{
|
42
|
|
|
$combinations[$i][] = $this->getElement($j, $value);
|
43
|
|
|
}
|
44
|
|
|
}
|
45
|
|
|
|
46
|
|
|
return $combinations;
|
47
|
|
|
}
|
48
|
|
|
|
49
|
|
|
protected function getElement($i, $j)
|
50
|
|
|
{
|
51
|
|
|
return $this->elements[array_keys($this->elements)[$i]][$j];
|
52
|
|
|
}
|
53
|
|
|
|
54
|
|
|
/**
|
55
|
|
|
* @return array
|
56
|
|
|
*/
|
57
|
|
|
protected function getCounter()
|
58
|
|
|
{
|
59
|
|
|
$counter = array();
|
60
|
|
|
$element = array_fill(0, count($this->lengths), 0);
|
61
|
|
|
|
62
|
|
|
for($i = 0; $i < $this->getCounterLimit(); $i++)
|
63
|
|
|
{
|
64
|
|
|
$combination = $element;
|
65
|
|
|
|
66
|
|
|
for($j = count($this->lengths), $number = $i; $j > 0, $number > 0; $j--, $number = $integer)
|
67
|
|
|
list($integer, $combination[$j - 1]) = $this->divQr($number, $this->lengths[$j - 1]);
|
|
|
|
|
68
|
|
|
|
69
|
|
|
$counter[] = $combination;
|
70
|
|
|
}
|
71
|
|
|
|
72
|
|
|
return $counter;
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
/**
|
76
|
|
|
* @return integer
|
77
|
|
|
*/
|
78
|
|
|
protected function getCounterLimit()
|
79
|
|
|
{
|
80
|
|
|
$limit = 1;
|
81
|
|
|
|
82
|
|
|
foreach($this->lengths as $length)
|
83
|
|
|
$limit *= $length;
|
84
|
|
|
|
85
|
|
|
return $limit;
|
86
|
|
|
}
|
87
|
|
|
|
88
|
|
|
/**
|
89
|
|
|
* Получение частного и остатка от деления
|
90
|
|
|
*
|
91
|
|
|
* @param integer $n
|
92
|
|
|
* @param integer $d
|
93
|
|
|
*
|
94
|
|
|
* @return array
|
95
|
|
|
*/
|
96
|
|
|
protected function divQr($n, $d)
|
97
|
|
|
{
|
98
|
|
|
return array(intval($n / $d), $n % $d);
|
99
|
|
|
}
|
100
|
|
|
} |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.