|
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 mixed Elemento da cercare nell'array |
|
13
|
|
|
* @param $array Array nel quale cercare |
|
14
|
|
|
* @param $key string 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
|
|
|
} |
|
30
|
|
|
} else { |
|
31
|
1 |
|
return false; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
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 mixed Elemento da cercare nell'array |
|
42
|
|
|
* @param $array Array nel quale cercare |
|
43
|
|
|
* @param $key mixed 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
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} else { |
|
62
|
2 |
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
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 array Chiave-valore da cercare |
|
74
|
|
|
* @return Mixed False se non trovato l'elemento, altrimenti l'indice in cui si trova 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
|
|
|
} else { |
|
92
|
|
|
$vettorerisultati = array_intersect($vettorerisultati, $trovato); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
$primo = false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
if ($vettorerisultati === false) { |
|
99
|
1 |
|
$risposta = false; |
|
100
|
1 |
|
} elseif ($tutti === false) { |
|
101
|
1 |
|
$risposta = reset($vettorerisultati); |
|
102
|
|
|
} 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 string Nome della chiave dell'array per cui ordinare |
|
114
|
|
|
* @param $type int 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
|
|
|
} |
|
130
|
1 |
|
$args[$n] = $tmp; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
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 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.