1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
class ArrayFunctions |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* La funzione cerca un valore $elem nell'array multidimensionale $array all'interno di ogni elemento con chiave $key di ogni riga di array |
10
|
|
|
* e restituisce l'indice. |
11
|
|
|
* |
12
|
|
|
* @param $elem Oggetto da cercare |
13
|
|
|
* @param $array Array nel quale cercare |
14
|
|
|
* @param $key Nome della chiave nella quale cercare $elem |
15
|
|
|
* |
16
|
|
|
* @return mixed False se non trovato l'elemento, altrimenti l'indice in cui si è trovato il valore |
17
|
|
|
*/ |
18
|
1 |
|
public static function inMultiarray($elem, $array, $key) |
19
|
|
|
{ |
20
|
1 |
|
foreach ($array as $indice => $value) { |
21
|
1 |
|
if (!is_array($value)) { |
22
|
|
|
return false; |
23
|
|
|
} |
24
|
1 |
|
if (array_key_exists($key, $value)) { |
25
|
1 |
|
foreach ($value as $nomecolonna => $colonna) { |
26
|
1 |
|
if ($colonna === $elem && $nomecolonna == $key) { |
27
|
1 |
|
return $indice; |
28
|
|
|
} |
29
|
1 |
|
} |
30
|
1 |
|
} else { |
31
|
|
|
return false; |
32
|
|
|
} |
33
|
1 |
|
} |
34
|
1 |
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* La funzione cerca un valore $elem nell'array multidimensionale $array all'interno di ogni elemento con chiave $key di ogni riga di array |
39
|
|
|
* e restituisce l'indice |
40
|
|
|
* |
41
|
|
|
* @param $elem Oggetto da cercare |
42
|
|
|
* @param $array Array nel quale cercare |
43
|
|
|
* @param $key Nome della chiave nella quale cercare $elem |
44
|
|
|
* @return Mixed False se non trovato l'elemento, altrimenti il vettore con tutti gli indici |
45
|
|
|
*/ |
46
|
2 |
|
public static function inMultiarrayTutti($elem, $array, $key) |
47
|
|
|
{ |
48
|
|
|
|
49
|
2 |
|
$trovato = array(); |
50
|
|
|
|
51
|
2 |
|
foreach ($array as $indice => $value) { |
52
|
2 |
|
if (!is_array($value)) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
2 |
|
if (array_key_exists($key, $value)) { |
56
|
2 |
|
foreach ($value as $nomecolonna => $colonna) { |
57
|
2 |
|
if ($colonna === $elem && $nomecolonna == $key) { |
58
|
2 |
|
$trovato[] = $indice; |
59
|
2 |
|
} |
60
|
2 |
|
} |
61
|
2 |
|
} else { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
2 |
|
} |
65
|
2 |
|
return (count($trovato) > 0 ? $trovato : false); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* La funzione cerca un valore $elem nell'array multidimensionale $array all'interno di ogni elemento con chiave $key di ogni riga di array |
70
|
|
|
* e restituisce l'indice |
71
|
|
|
* |
72
|
|
|
* @param $array Array nel quale cercare |
73
|
|
|
* @param $search Chiave-valore da cercare |
74
|
|
|
* @return Mixed False se non trovato l'elemento, altrimenti l'indice in cui si è trovato il valore |
75
|
|
|
*/ |
76
|
1 |
|
public static function multiInMultiarray($array, $search, $debug = false, $tutti = false) |
77
|
|
|
{ |
78
|
1 |
|
$primo = true; |
79
|
1 |
|
$vettorerisultati = array(); |
80
|
|
|
|
81
|
1 |
|
foreach ($search as $key => $singolaricerca) { |
82
|
1 |
|
$trovato = self::inMultiarrayTutti($singolaricerca, $array, $key, $debug); |
|
|
|
|
83
|
|
|
|
84
|
1 |
|
if ($trovato === false) { |
85
|
1 |
|
$vettorerisultati = false; |
86
|
1 |
|
break; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
if ($primo) { |
90
|
1 |
|
$vettorerisultati = $trovato; |
91
|
1 |
|
} else { |
92
|
|
|
$vettorerisultati = array_intersect($vettorerisultati, $trovato); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
$primo = false; |
96
|
1 |
|
} |
97
|
|
|
|
98
|
1 |
|
if ($vettorerisultati === false) { |
99
|
1 |
|
$risposta = false; |
100
|
1 |
|
} elseif ($tutti === false) { |
101
|
1 |
|
$risposta = reset($vettorerisultati); |
102
|
1 |
|
} else { |
103
|
1 |
|
$risposta = $vettorerisultati; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return $risposta; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* La funzione ordina un array multidimensionale $array. |
111
|
|
|
* |
112
|
|
|
* @param $array Array da ordinare |
113
|
|
|
* @param $key Nome della chiave dell'array per cui ordinare |
114
|
|
|
* @param $type Tipo di ordinamento SORT_ASC, SORT_DESC |
115
|
|
|
* |
116
|
|
|
* @return array Ritorna l'array ordinato |
117
|
|
|
* |
118
|
|
|
* @example arrayOrderby($rubrica,"cognome",SORT_ASC);<br/>$rubrica = array();<br/>$rubrica[] = array("matricola" => 99999, "cognome" => "rossi", "nome" => "mario");<br/>$rubrica[] = array("matricola" => 99998, "cognome" => "bianchi", "nome" => "andrea");<br/>$rubrica[] = array("matricola" => 99997, "cognome" => "verdi", "nome" => "michele");<br/>rusulterà<br/>$rubrica[0]("matricola"=>99998,"cognome"=>"bianchi","nome"=>"andrea")<br/>$rubrica[1]("matricola"=>99999,"cognome"=>"rossi","nome"=>"mario")<br/>$rubrica[2]("matricola"=>99997,"cognome"=>"verdi","nome"=>"michele")<br/> |
119
|
|
|
*/ |
120
|
1 |
|
public static function arrayOrderby() |
121
|
|
|
{ |
122
|
1 |
|
$args = func_get_args(); |
123
|
1 |
|
$data = array_shift($args); |
124
|
1 |
|
foreach ($args as $n => $field) { |
125
|
1 |
|
if (is_string($field)) { |
126
|
1 |
|
$tmp = array(); |
127
|
1 |
|
foreach ($data as $key => $row) { |
128
|
1 |
|
$tmp[$key] = $row[$field]; |
129
|
1 |
|
} |
130
|
1 |
|
$args[$n] = $tmp; |
131
|
1 |
|
} |
132
|
1 |
|
} |
133
|
1 |
|
$args[] = &$data; |
134
|
1 |
|
call_user_func_array('array_multisort', $args); |
135
|
1 |
|
return array_pop($args); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function arraySearchRecursive($needle, $haystack) |
139
|
|
|
{ |
140
|
|
|
foreach ($haystack as $key => $val) { |
141
|
|
|
if (stripos(implode('', $val), $needle) > 0) { |
142
|
|
|
return $key; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.