-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.php
More file actions
44 lines (36 loc) · 1.07 KB
/
Copy pathsearch.php
File metadata and controls
44 lines (36 loc) · 1.07 KB
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
<?php
header('Content-Type: application/json; charset=utf-8');
$texto = $_GET['texto'] ?? '';
if (empty($texto)) {
echo json_encode(['libros' => [], 'autores' => []]);
exit;
}
$dataset = json_decode(file_get_contents(__DIR__ . '/dataset.json'), true);
if (!$dataset) {
http_response_code(500);
echo json_encode(['error' => 'Dataset no disponible']);
exit;
}
$libros = [];
$autoresMap = [];
foreach ($dataset as $libro) {
if ($libro['titulo'] === $texto) {
$libros[] = $libro;
}
if ($libro['autor'] === $texto) {
$autoresMap[$libro['autor']] = true;
}
}
$autores = [];
foreach (array_keys($autoresMap) as $nombreAutor) {
$librosAutor = array_values(array_filter(
$dataset,
fn($l) => $l['autor'] === $nombreAutor
));
usort($librosAutor, fn($a, $b) => strcmp($b['fecha_nov'], $a['fecha_nov']));
$autores[] = [
'autor' => $nombreAutor,
'ultimos_libros' => array_slice($librosAutor, 0, 2),
];
}
echo json_encode(['libros' => $libros, 'autores' => $autores], JSON_UNESCAPED_UNICODE);