/
ejemplo1.php
320 lines (315 loc) · 10.3 KB
/
ejemplo1.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
echo '<h1>Código de Árbol en PHP para transformar respuesta SQL en JSON</h1>';
/*
* Se llaman las funciones del archivo tree.php
*/
require 'tree.php';
/*
* Esta función construye los atributos de la jerarquía JSON haciendo foreach a los niveles del árbol.
* Esta función no es dinámica, por tanto hay que saber el número de niveles y los campos que trae la consulta SQL.
*/
function crearArregloPorNiveles($arbol,$filas,$idNiveles){
$response = array();
foreach ($arbol as $nivel1 => $valnivel1) {
$fila = array_findUnique($filas, function($k) use ($idNiveles,$nivel1) {
return $k[$idNiveles[0]] == $nivel1;
});
array_push($response, array (
'id_modalidad' => $fila['id_modalidad'],
'descripcion' => $fila['descripcion_modalidad'],
'sede' => array()
));
foreach ($valnivel1 as $nivel2 => $valnivel2) {
$fila = array_findUnique($filas, function($k) use ($idNiveles,$nivel1,$nivel2) {
return ($k[$idNiveles[0]] == $nivel1 && $k[$idNiveles[1]] == $nivel2);
});
array_push($response[count($response)-1]['sede'], array (
'id_sede' => $fila['id_sede'],
'abreviatura' => $fila['abreviatura_sede'],
'consultorio' => array(),
'profesional' => array()
));
foreach ($valnivel2 as $nivel3 => $valnivel3) {
$fila = array_findUnique($filas, function($k) use ($idNiveles,$nivel1,$nivel2,$nivel3) {
return ($k[$idNiveles[0]] == $nivel1 && $k[$idNiveles[1]] == $nivel2 && $k[$idNiveles[2]] == $nivel3);
});
array_push($response[count($response)-1]['sede'][count($response[count($response)-1]['sede'])-1]['consultorio'], array (
'id_consultorio' => $fila['id_consultorio'],
'nombre' => $fila['nombre_consultorio']
));
array_push($response[count($response)-1]['sede'][count($response[count($response)-1]['sede'])-1]['profesional'], array (
'id_profesional' => $fila['id_profesional'],
'nombre_persona' => $fila['nombre_persona']
));
}
}
}
return $response;
}
/*
* Con esta funcion se obtiene como respuesta un Objeto en PHP para ser exportado a JSON.
*/
function unificarResultadosPorNiveles($rows, $idNiveles){
$arbol = crearArbol($rows,$idNiveles);
$response = crearArregloPorNiveles($arbol,$rows,$idNiveles);
return $response;
}
/*
* Esta es una variable que simula el resultado de la respuesta de una consulta.
*/
$resultadoSQL = array(
0 => array(
0 => '1',
'id_modalidad' => '1',
1 => 'MEDICINA',
'descripcion_modalidad' => 'MEDICINA',
2 => '1',
'id_sede' => '1',
3 => 'FCMA',
'abreviatura_sede' => 'FCMA',
4 => '1',
'id_consultorio' => '1',
5 => 'consultorio 1',
'nombre_consultorio' => 'consultorio 1',
6 => '1',
'id_profesional' => '1',
7 => '1000000000',
'documento_persona' => '1000000000',
8 => 'NEIFY NATHALYA USECHE CUELLAR',
'nombre_persona' => 'NEIFY NATHALYA USECHE CUELLAR'
),
1 => array(
0 => '1',
'id_modalidad' => '1',
1 => 'MEDICINA',
'descripcion_modalidad' => 'MEDICINA',
2 => '4',
'id_sede' => '4',
3 => 'FCMB',
'abreviatura_sede' => 'FCMB',
4 => '3',
'id_consultorio' => '3',
5 => 'consultorio 1',
'nombre_consultorio' => 'consultorio 1',
6 => '1',
'id_profesional' => '1',
7 => '1000000000',
'documento_persona' => '1000000000',
8 => 'NEIFY NATHALYA USECHE CUELLAR',
'nombre_persona' => 'NEIFY NATHALYA USECHE CUELLAR'
),
2 => array(
0 => '2',
'id_modalidad' => '2',
1 => 'ODONTOLOGÍA',
'descripcion_modalidad' => 'ODONTOLOGÍA',
2 => '1',
'id_sede' => '1',
3 => 'FCMA',
'abreviatura_sede' => 'FCMA',
4 => '1',
'id_consultorio' => '1',
5 => 'consultorio 1',
'nombre_consultorio' => 'consultorio 1',
6 => '1',
'id_profesional' => '1',
7 => '1000000000',
'documento_persona' => '1000000000',
8 => 'NEIFY NATHALYA USECHE CUELLAR',
'nombre_persona' => 'NEIFY NATHALYA USECHE CUELLAR'
),
3 => array(
0 => '2',
'id_modalidad' => '2',
1 => 'ODONTOLOGÍA',
'descripcion_modalidad' => 'ODONTOLOGÍA',
2 => '2',
'id_sede' => '2',
3 => 'FMVI',
'abreviatura_sede' => 'FMVI',
4 => '2',
'id_consultorio' => '2',
5 => 'consultorio 2',
'nombre_consultorio' => 'consultorio 2',
6 => '2',
'id_profesional' => '2',
7 => 'E01010101',
'documento_persona' => 'E01010101',
8 => 'NEIFY CUELLAR',
'nombre_persona' => 'NEIFY CUELLAR'
),
4 => array(
0 => '2',
'id_modalidad' => '2',
1 => 'ODONTOLOGÍA',
'descripcion_modalidad' => 'ODONTOLOGÍA',
2 => '4',
'id_sede' => '4',
3 => 'FCMB',
'abreviatura_sede' => 'FCMB',
4 => '3',
'id_consultorio' => '3',
5 => 'consultorio 1',
'nombre_consultorio' => 'consultorio 1',
6 => '1',
'id_profesional' => '1',
7 => '1000000000',
'documento_persona' => '1000000000',
8 => 'NEIFY NATHALYA USECHE CUELLAR',
'nombre_persona' => 'NEIFY NATHALYA USECHE CUELLAR'
),
5 => array(
0 => '2',
'id_modalidad' => '2',
1 => 'ODONTOLOGÍA',
'descripcion_modalidad' => 'ODONTOLOGÍA',
2 => '4',
'id_sede' => '4',
3 => 'FCMB',
'abreviatura_sede' => 'FCMB',
4 => '3',
'id_consultorio' => '3',
5 => 'consultorio 1',
'nombre_consultorio' => 'consultorio 1',
6 => '2',
'id_profesional' => '2',
7 => 'E01010101',
'documento_persona' => 'E01010101',
8 => 'NEIFY CUELLAR',
'nombre_persona' => 'NEIFY CUELLAR'
),
6 => array(
0 => '3',
'id_modalidad' => '3',
1 => 'PSICOLOGÍA',
'descripcion_modalidad' => 'PSICOLOGÍA',
2 => '1',
'id_sede' => '1',
3 => 'FCMA',
'abreviatura_sede' => 'FCMA',
4 => '1',
'id_consultorio' => '1',
5 => 'consultorio 1',
'nombre_consultorio' => 'consultorio 1',
6 => '1',
'id_profesional' => '1',
7 => '1000000000',
'documento_persona' => '1000000000',
8 => 'NEIFY NATHALYA USECHE CUELLAR',
'nombre_persona' => 'NEIFY NATHALYA USECHE CUELLAR'
),
7 => array(
0 => '3',
'id_modalidad' => '3',
1 => 'PSICOLOGÍA',
'descripcion_modalidad' => 'PSICOLOGÍA',
2 => '1',
'id_sede' => '1',
3 => 'FCMA',
'abreviatura_sede' => 'FCMA',
4 => '1',
'id_consultorio' => '1',
5 => 'consultorio 1',
'nombre_consultorio' => 'consultorio 1',
6 => '3',
'id_profesional' => '3',
7 => '10101010',
'documento_persona' => '10101010',
8 => 'ULISES USECHE DELGADO',
'nombre_persona' => 'ULISES USECHE DELGADO'
),
8 => array(
0 => '3',
'id_modalidad' => '3',
1 => 'PSICOLOGÍA',
'descripcion_modalidad' => 'PSICOLOGÍA',
2 => '4',
'id_sede' => '4',
3 => 'FCMB',
'abreviatura_sede' => 'FCMB',
4 => '3',
'id_consultorio' => '3',
5 => 'consultorio 1',
'nombre_consultorio' => 'consultorio 1',
6 => '1',
'id_profesional' => '1',
7 => '1000000000',
'documento_persona' => '1000000000',
8 => 'NEIFY NATHALYA USECHE CUELLAR',
'nombre_persona' => 'NEIFY NATHALYA USECHE CUELLAR'
),
9 => array(
0 => '3',
'id_modalidad' => '3',
1 => 'PSICOLOGÍA',
'descripcion_modalidad' => 'PSICOLOGÍA',
2 => '4',
'id_sede' => '4',
3 => 'FCMB',
'abreviatura_sede' => 'FCMB',
4 => '3',
'id_consultorio' => '3',
5 => 'consultorio 1',
'nombre_consultorio' => 'consultorio 1',
6 => '3',
'id_profesional' => '3',
7 => '10101010',
'documento_persona' => '10101010',
8 => 'ULISES USECHE DELGADO',
'nombre_persona' => 'ULISES USECHE DELGADO'
),
10 => array(
0 => '4',
'id_modalidad' => '4',
1 => 'FISIOTERAPIA',
'descripcion_modalidad' => 'FISIOTERAPIA',
2 => '100',
'id_sede' => '100',
3 => 'FCPO',
'abreviatura_sede' => 'FCPO',
4 => '4',
'id_consultorio' => '4',
5 => 'consultorio 1 postgrados',
'nombre_consultorio' => 'consultorio 1 postgrados',
6 => '4',
'id_profesional' => '4',
7 => '92100950407',
'documento_persona' => '92100950407',
8 => 'KAREN MARIANA USECHE CUELLAR',
'nombre_persona' => 'KAREN MARIANA USECHE CUELLAR'
),
11 => array(
0 => '4',
'id_modalidad' => '4',
1 => 'FISIOTERAPIA',
'descripcion_modalidad' => 'FISIOTERAPIA',
2 => '100',
'id_sede' => '100',
3 => 'FCPO',
'abreviatura_sede' => 'FCPO',
4 => '5',
'id_consultorio' => '5',
5 => 'consultorio 4',
'nombre_consultorio' => 'consultorio 4',
6 => '4',
'id_profesional' => '4',
7 => '92100950407',
'documento_persona' => '92100950407',
8 => 'KAREN MARIANA USECHE CUELLAR',
'nombre_persona' => 'KAREN MARIANA USECHE CUELLAR'
)
);
/*
* Las llaves de la consulta jerárquica se guardan en orden en un arreglo.
*/
$idsKeysSQL = array('id_modalidad','id_sede','id_profesional');
/*
* Se ingresan los parámetros de Filas del SQL y las respectivas llaves que se establecieron en la consulta.
*/
$resultadoArray = unificarResultadosPorNiveles($resultadoSQL,$idsKeysSQL);
/*
* Se imprime en pantalla en código en JSON.
*/
echo '<pre>';
echo json_encode($resultadoArray, JSON_PRETTY_PRINT);
echo '</pre>';