forked from polarblau/suggest
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnested_search.php
76 lines (72 loc) · 1.15 KB
/
nested_search.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
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$ingredients = [
[
'id'=> 1,
'name' => 'Apple',
], [
'id'=> 2,
'name' => 'Appricot',
], [
'id'=> 3,
'name' => 'Almond',
], [
'id'=> 4,
'name' => 'Asparagus',
], [
'id'=> 5,
'name' => 'Banana',
], [
'id'=> 6,
'name' => 'Black Berry',
], [
'id'=> 7,
'name' => 'Beer',
], [
'id'=> 8,
'name' => 'Bastard Sugar',
], [
'id'=> 9,
'name' => 'Beef',
], [
'id'=> 10,
'name' => 'Cider',
], [
'id'=> 11,
'name' => 'Cinamon',
], [
'id'=> 12,
'name' => 'Chili',
], [
'id'=> 13,
'name' => 'Curry',
], [
'id'=> 14,
'name' => 'Chicken',
], [
'id'=> 15,
'name' => 'Durian',
], [
'id'=> 16,
'name' => 'Date',
], [
'id'=> 17,
'name' => 'Egg',
], [
'id'=> 18,
'name' => 'Eggplant',
], [
'id'=> 19,
'name' => 'Fig'
]
];
$searchString = $_GET['q'];
$input = preg_quote($searchString, '/'); // don't forget to quote input string!
$result = array_filter($ingredients, function ($value) use ($input) {
if (preg_match('/^' . $input . '/i', $value['name'])) {
return true;
}
return false;
});
echo json_encode(array_values($result));