1 | <?php |
||
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 | public static function inMultiarray($elem, $array, $key) |
||
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 | public static function inMultiarrayTutti($elem, $array, $key, $debug) |
||
47 | { |
||
48 | |||
49 | /*if ($debug) { |
||
|
|||
50 | var_dump($elem); |
||
51 | |||
52 | var_dump($key); |
||
53 | }*/ |
||
54 | |||
55 | |||
56 | $trovato = array(); |
||
57 | |||
58 | foreach ($array as $indice => $value) { |
||
59 | if (!is_array($value)) { |
||
60 | return false; |
||
61 | } |
||
62 | if (array_key_exists($key, $value)) { |
||
63 | foreach ($value as $nomecolonna => $colonna) { |
||
64 | if ($colonna === $elem && $nomecolonna == $key) { |
||
65 | if ($debug) { |
||
66 | echo "$colonna = $elem quando indice = $indice \n"; |
||
67 | echo "$nomecolonna = $key quando indice = $indice \n"; |
||
68 | var_dump($array[$indice]); |
||
69 | echo "\n"; |
||
70 | } |
||
71 | |||
72 | $trovato[] = $indice; |
||
73 | } |
||
74 | } |
||
75 | } else { |
||
76 | return false; |
||
77 | } |
||
78 | } |
||
79 | return (count($trovato) > 0 ? $trovato : false); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * La funzione cerca un valore $elem nell'array multidimensionale $array all'interno di ogni elemento con chiave $key di ogni riga di array |
||
84 | * e restituisce l'indice |
||
85 | * |
||
86 | * @param $array Array nel quale cercare |
||
87 | * @param $search Chiave-valore da cercare |
||
88 | * @return Mixed False se non trovato l'elemento, altrimenti l'indice in cui si è trovato il valore |
||
89 | */ |
||
90 | public static function multiInMultiarray($array, $search, $debug = false, $tutti = false) |
||
91 | { |
||
92 | $primo = true; |
||
93 | $vettorerisultati = array(); |
||
94 | |||
95 | /*if ($debug) { |
||
96 | echo "<br>\n vettore search <br>\n"; |
||
97 | var_dump($search); |
||
98 | echo "<br>\n fine vettore search <br>\n"; |
||
99 | |||
100 | echo "<br>\n vettorecompleto <br>\n"; |
||
101 | var_dump($array); |
||
102 | echo "<br>\n fine vettorecompleto <br>\n"; |
||
103 | }*/ |
||
104 | |||
105 | |||
106 | foreach ($search as $key => $singolaricerca) { |
||
107 | $trovato = self::inMultiarrayTutti($singolaricerca, $array, $key, $debug); |
||
108 | |||
109 | if ($debug) { |
||
110 | echo $primo ? "<br>\n primo <br>\n" : "<br>\n non primo <br>\n"; |
||
111 | var_dump($trovato); |
||
112 | echo $primo ? "<br>\n fine primo <br>\n" : "<br>\n fine non primo <br>\n"; |
||
113 | } |
||
114 | |||
115 | if ($trovato === false) { |
||
116 | $vettorerisultati = false; |
||
117 | break; |
||
118 | } |
||
119 | |||
120 | if ($primo) { |
||
121 | $vettorerisultati = $trovato; |
||
122 | } else { |
||
123 | $vettorerisultati = array_intersect($vettorerisultati, $trovato); |
||
124 | /*if ($debug) { |
||
125 | echo "<br>\n vettorerisultati<br>\n"; |
||
126 | var_dump($vettorerisultati); |
||
127 | echo "<br>\n fine vettorerisultati<br>\n"; |
||
128 | }*/ |
||
129 | } |
||
130 | |||
131 | $primo = false; |
||
132 | } |
||
133 | |||
134 | if ($vettorerisultati === false) { |
||
135 | $risposta = false; |
||
136 | } elseif ($tutti === false) { |
||
137 | $risposta = reset($vettorerisultati); |
||
138 | } else { |
||
139 | $risposta = $vettorerisultati; |
||
140 | } |
||
141 | |||
142 | return $risposta; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * La funzione ordina un array multidimensionale $array. |
||
147 | * |
||
148 | * @param $array Array da ordinare |
||
149 | * @param $key Nome della chiave dell'array per cui ordinare |
||
150 | * @param $type Tipo di ordinamento SORT_ASC, SORT_DESC |
||
151 | * |
||
152 | * @return array Ritorna l'array ordinato |
||
153 | * |
||
154 | * @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/> |
||
155 | */ |
||
156 | public static function arrayOrderby() |
||
173 | |||
174 | public function arraySearchRecursive($needle, $haystack) |
||
184 | } |
||
185 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.