-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArraySearch.php
More file actions
86 lines (84 loc) · 4.3 KB
/
ArraySearch.php
File metadata and controls
86 lines (84 loc) · 4.3 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
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
<?php
/**
*
* ArraySearch
*
* Created for to do search inside of array.
* if $all is 1, all results will return by array
*
* Example: ArraySearch( array $dizi, string "key1 = 'bunaesitolmali1' and key2 >= 'bundanbuyukolmali' or key3 != 'bunaesitolmasin3'", int $all = 0 );
*
*/
function ArraySearch($SearchArray, $query, $all = 0, $Return = 'direct')
{
$SearchArray = json_decode(json_encode($SearchArray), true);
$ResultArray = array();
if (is_array($SearchArray)) {
$desen = "@[\s*]?[\'{1}]?([a-zA-Z\ç\Ç\ö\Ö\ş\Ş\ı\İ\ğ\Ğ\ü\Ü[:space:]0-9-_]*)[\'{1}]?[\s*]?(\<\=|\>\=|\=|\!\=|\<|\>)\s*\'([a-zA-Z\ç\Ç\ö\Ö\ş\Ş\ı\İ\ğ\Ğ\ü\Ü[:space:]0-9-_:]*)\'[\s*]?(and|or|\&\&|\|\|)?@si";
$DonenSonuc = preg_match_all($desen, $query, $Result);
if ($DonenSonuc) {
foreach ($SearchArray as $i => $ArrayElement) {
if (is_array($ArrayElement)) {
$SearchStatus = 0;
$EvalString = "";
for ($r = 0; $r < count($Result[1]); $r++):
if ($Result[2][$r] == '=') {
$Operator = "==";
} elseif ($Result[2][$r] == '!=') {
$Operator = "!=";
} elseif ($Result[2][$r] == '>=') {
$Operator = ">=";
} elseif ($Result[2][$r] == '<=') {
$Operator = "<=";
} elseif ($Result[2][$r] == '>') {
$Operator = ">";
} elseif ($Result[2][$r] == '<') {
$Operator = "<";
} else {
$Operator = "==";
}
$AndOperator = "";
if ($r != count($Result[1]) - 1) {
$AndOperator = $Result[4][$r] ?: 'and';
}
$EvalString .= '("' . $ArrayElement[$Result[1][$r]] . '"' . $Operator . '"' . $Result[3][$r] . '") ' . $AndOperator . ' ';
endfor;
eval('if( ' . $EvalString . ' ) $SearchStatus = 1;');
if ($SearchStatus === 1) {
if ($all === 1) {
if ($Return == 'direct') :
$ResultArray[$i] = is_array($ResultArray[$i]) ? $ResultArray[$i] : [];
$ResultArray[$i] = array_merge($ResultArray[$i], $ArrayElement);
elseif ($Return == 'array') :
$ResultArray['index'][] = $i;
$ResultArray['array'] = is_array($ResultArray['array']) ? $ResultArray['array'] : [];
$ResultArray['array'] = array_merge($ResultArray['array'], $ArrayElement);
endif;
} else {
if ($Return == 'direct') :
$ResultArray = $i;
elseif ($Return == 'array') :
$ResultArray['index'] = $i;
endif;
return $ResultArray;
}
}
if ($all === 1 && is_array($ArrayElement)) {
if ($Return == 'direct') :
$args = func_get_args();
$ChildResult = ArraySearch($ArrayElement, $args[1], $args[2], $args[3]);
if (count($ChildResult) > 0):
$ResultArray[$i] = is_array($ResultArray[$i]) ? $ResultArray[$i] : [];
$ResultArray[$i] = array_merge($ResultArray[$i], $ChildResult);
endif;
endif;
}
}
}
if ($all === 1) {
return $ResultArray;
}
}
}
return false;
}