1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Utils\Arrays; |
4
|
|
|
|
5
|
|
|
class ArrayUtils |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* La funzione cerca un valore $elem nell'array multidimensionale $array all'interno di ogni elemento con chiave $key di ogni riga di array |
9
|
|
|
* e restituisce l'indice. |
10
|
|
|
* |
11
|
|
|
* @param $elem mixed Elemento da cercare nell'array |
12
|
|
|
* @param $array Array nel quale cercare |
13
|
|
|
* @param $key string Nome della chiave nella quale cercare $elem |
14
|
|
|
* |
15
|
|
|
* @return mixed False se non trovato l'elemento, altrimenti l'indice in cui si è trovato il valore |
16
|
|
|
*/ |
17
|
1 |
|
public static function inMultiarray($elem, $array, $key) |
18
|
|
|
{ |
19
|
1 |
|
foreach ($array as $indice => $value) { |
20
|
1 |
|
if (!is_array($value)) { |
21
|
|
|
return false; |
22
|
|
|
} |
23
|
1 |
|
if (array_key_exists($key, $value)) { |
24
|
1 |
|
foreach ($value as $nomecolonna => $colonna) { |
25
|
1 |
|
if ($colonna === $elem && $nomecolonna == $key) { |
26
|
1 |
|
return $indice; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} else { |
30
|
1 |
|
return false; |
31
|
|
|
} |
32
|
|
|
} |
33
|
1 |
|
return false; |
34
|
|
|
} |
35
|
|
|
/** |
36
|
|
|
* La funzione cerca un valore $elem nell'array multidimensionale $array all'interno di ogni elemento con chiave $key di ogni riga di array |
37
|
|
|
* e restituisce l'indice |
38
|
|
|
* |
39
|
|
|
* @param $elem mixed Elemento da cercare nell'array |
40
|
|
|
* @param $array Array nel quale cercare |
41
|
|
|
* @param $key mixed Nome della chiave nella quale cercare $elem |
42
|
|
|
* @return Mixed False se non trovato l'elemento, altrimenti il vettore con tutti gli indici |
43
|
|
|
*/ |
44
|
2 |
|
public static function inMultiarrayTutti($elem, $array, $key) |
45
|
|
|
{ |
46
|
|
|
|
47
|
2 |
|
$trovato = array(); |
48
|
|
|
|
49
|
2 |
|
foreach ($array as $indice => $value) { |
50
|
2 |
|
if (!is_array($value)) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
2 |
|
if (array_key_exists($key, $value)) { |
54
|
2 |
|
foreach ($value as $nomecolonna => $colonna) { |
55
|
2 |
|
if ($colonna === $elem && $nomecolonna == $key) { |
56
|
2 |
|
$trovato[] = $indice; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
2 |
|
return false; |
61
|
|
|
} |
62
|
|
|
} |
63
|
2 |
|
return (count($trovato) > 0 ? $trovato : false); |
64
|
|
|
} |
65
|
|
|
/** |
66
|
|
|
* La funzione cerca un valore $elem nell'array multidimensionale $array all'interno di ogni elemento con chiave $key di ogni riga di array |
67
|
|
|
* e restituisce l'indice |
68
|
|
|
* |
69
|
|
|
* @param $array Array nel quale cercare |
70
|
|
|
* @param $search array Chiave-valore da cercare |
71
|
|
|
* @return Mixed False se non trovato l'elemento, altrimenti l'indice in cui si trova il valore |
72
|
|
|
*/ |
73
|
1 |
|
public static function multiInMultiarray($array, $search, $debug = false, $tutti = false) |
74
|
|
|
{ |
75
|
1 |
|
$primo = true; |
|
|
|
|
76
|
1 |
|
$vettorerisultati = array(); |
77
|
|
|
|
78
|
1 |
|
foreach ($search as $key => $singolaricerca) { |
79
|
1 |
|
$trovato = self::inMultiarrayTutti($singolaricerca, $array, $key, $debug); |
|
|
|
|
80
|
|
|
|
81
|
1 |
|
if ($trovato === false) { |
82
|
1 |
|
$vettorerisultati = false; |
83
|
1 |
|
break; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
if ($primo) { |
87
|
1 |
|
$vettorerisultati = $trovato; |
88
|
|
|
} else { |
89
|
|
|
$vettorerisultati = array_intersect($vettorerisultati, $trovato); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
$primo = false; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
if ($vettorerisultati === false) { |
96
|
1 |
|
$risposta = false; |
97
|
1 |
|
} elseif ($tutti === false) { |
98
|
1 |
|
$risposta = reset($vettorerisultati); |
99
|
|
|
} else { |
100
|
1 |
|
$risposta = $vettorerisultati; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return $risposta; |
104
|
|
|
} |
105
|
|
|
/** |
106
|
|
|
* La funzione ordina un array multidimensionale $array. |
107
|
|
|
* |
108
|
|
|
* @param $array Array da ordinare |
109
|
|
|
* @param $key string Nome della chiave dell'array per cui ordinare |
110
|
|
|
* @param $type int Tipo di ordinamento SORT_ASC, SORT_DESC |
111
|
|
|
* |
112
|
|
|
* @return array Ritorna l'array ordinato |
113
|
|
|
* |
114
|
|
|
* @example arrayOrderby($rubrica,"cognome",SORT_ASC); |
115
|
|
|
* <br/>$rubrica = array();<br/> |
116
|
|
|
* $rubrica[] = array("matricola" => 99999, "cognome" => "rossi", "nome" => "mario");<br/> |
117
|
|
|
* $rubrica[] = array("matricola" => 99998, "cognome" => "bianchi", "nome" => "andrea"); |
118
|
|
|
* $rubrica[] = array("matricola" => 99997, "cognome" => "verdi", "nome" => "michele"); |
119
|
|
|
* rusulterà<br/>$rubrica[0]("matricola"=>99998,"cognome"=>"bianchi","nome"=>"andrea") |
120
|
|
|
* $rubrica[1]("matricola"=>99999,"cognome"=>"rossi","nome"=>"mario") |
121
|
|
|
* $rubrica[2]("matricola"=>99997,"cognome"=>"verdi","nome"=>"michele") |
122
|
|
|
*/ |
123
|
1 |
|
public static function arrayOrderby() |
124
|
|
|
{ |
125
|
1 |
|
$args = func_get_args(); |
126
|
1 |
|
$data = array_shift($args); |
127
|
1 |
|
foreach ($args as $n => $field) { |
128
|
1 |
|
if (is_string($field)) { |
129
|
1 |
|
$tmp = array(); |
130
|
1 |
|
foreach ($data as $key => $row) { |
131
|
1 |
|
$tmp[$key] = $row[$field]; |
132
|
|
|
} |
133
|
1 |
|
$args[$n] = $tmp; |
134
|
|
|
} |
135
|
|
|
} |
136
|
1 |
|
$args[] = &$data; |
137
|
1 |
|
call_user_func_array('array_multisort', $args); |
138
|
1 |
|
return array_pop($args); |
139
|
|
|
} |
140
|
|
|
public function arraySearchRecursive($needle, $haystack) |
141
|
|
|
{ |
142
|
|
|
foreach ($haystack as $key => $val) { |
143
|
|
|
if (stripos(implode('', $val), $needle) > 0) { |
144
|
|
|
return $key; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
/** |
151
|
|
|
* La funzione ordina un array multidimensionale che ha per indice chiavi associative. |
152
|
|
|
* |
153
|
|
|
* @param $array Array da ordinare |
154
|
|
|
* @param $subkey string Nome della chiave dell'array associato alla chiave per cui ordinare |
155
|
|
|
* @param $sort_ascending boolean Tipo di ordinamento true ASC, false DESC |
156
|
|
|
* |
157
|
|
|
* @return array Ritorna l'array ordinato |
158
|
|
|
* |
159
|
|
|
* @example sortMultiAssociativeArray($rubrica, "ordine", true);<code> |
160
|
|
|
* array["nominativo" => ["nomecampo" => "nominativo","ordine" => 30],<br/><br/> |
161
|
|
|
* "datanascita" => ["nomecampo" => "datanascita","ordine" => 10],<br/><br/> |
162
|
|
|
* "telefono" => ["nomecampo" => "telefono","ordine" => 20]<br/><br/> |
163
|
|
|
* ritorna:<br/><br/> |
164
|
|
|
* array["datanascita" => ["nomecampo" => "datanascita","ordine" => 10],<br/><br/> |
165
|
|
|
* "telefono" => ["nomecampo" => "telefono","ordine" => 20],<br/><br/> |
166
|
|
|
* "nominativo" => ["nomecampo" => "nominativo","ordine" => 30]</code> |
167
|
|
|
* |
168
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
169
|
|
|
*/ |
170
|
13 |
|
public static function sortMultiAssociativeArray(&$array, $subkey, $sort_ascending = true) |
171
|
|
|
{ |
172
|
|
|
//Se sono tutti uguali (stesso "peso") evita di fare l'ordinamento |
173
|
13 |
|
if (!self::isSortArray($array, $subkey)) { |
174
|
13 |
|
if (count($array)) { |
175
|
13 |
|
$temp_array[key($array)] = array_shift($array); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
13 |
|
foreach ($array as $key => $val) { |
179
|
13 |
|
$offset = 0; |
180
|
13 |
|
$found = false; |
|
|
|
|
181
|
13 |
|
foreach ($temp_array as $tmp_key => $tmp_val) { |
182
|
13 |
|
if (!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey])) { |
|
|
|
|
183
|
13 |
|
$temp_array = array_merge( |
184
|
13 |
|
(array) array_slice($temp_array, 0, $offset), |
|
|
|
|
185
|
13 |
|
array($key => $val), |
186
|
13 |
|
array_slice($temp_array, $offset) |
187
|
|
|
); |
188
|
13 |
|
$found = true; |
|
|
|
|
189
|
|
|
} |
190
|
13 |
|
$offset++; |
191
|
|
|
} |
192
|
13 |
|
if (!$found) { |
193
|
13 |
|
$temp_array = array_merge($temp_array, array($key => $val)); |
194
|
|
|
} |
195
|
|
|
} |
196
|
13 |
|
$array = self::executeSortMultiAssociativeArray($temp_array, $sort_ascending); |
197
|
|
|
} |
198
|
13 |
|
} |
199
|
13 |
|
private static function isSortArray($array, $subkey) |
200
|
|
|
{ |
201
|
13 |
|
$check = null; |
202
|
13 |
|
$diff = false; |
|
|
|
|
203
|
13 |
|
$key = ""; |
|
|
|
|
204
|
|
|
//Controlla se sono tutti uguali i valori per i quali deve fare l'ordinamento |
205
|
13 |
|
foreach ($array as $key => $val) { |
206
|
13 |
|
if (isset($check) && $check != $val[$subkey]) { |
207
|
13 |
|
$diff = true; |
208
|
13 |
|
break; |
209
|
|
|
} else { |
210
|
13 |
|
$check = $val[$subkey]; |
211
|
|
|
} |
212
|
|
|
} |
213
|
13 |
|
return !$diff; |
214
|
|
|
} |
215
|
13 |
|
private static function executeSortMultiAssociativeArray($temp_array, $sort_ascending) |
216
|
|
|
{ |
217
|
13 |
|
if ($sort_ascending) { |
218
|
13 |
|
$array = array_reverse($temp_array); |
219
|
|
|
} else { |
220
|
1 |
|
$array = $temp_array; |
221
|
|
|
} |
222
|
13 |
|
return $array; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.