php 数组查询
<?php
function
arr_search
(
$array
,
$expression
) {
$result
= array();
$expression
=
preg_replace
(
"/([^/s]+?)(=|<|>|!)/"
,
"/$a['$1']$2"
,
$expression
);
foreach (
$array
as
$a
) if ( eval (
"return $expression;"
) )
$result
[] =
$a
;
return
$result
;
}
$data
= array (
array (
"name"
=>
"bill"
,
"age"
=>
40
),
array (
"name"
=>
"john"
,
"age"
=>
30
),
array (
"name"
=>
"jack"
,
"age"
=>
50
),
array (
"name"
=>
"john"
,
"age"
=>
25
)
);
print_r
(
arr_search
(
$data
,
"age>=30"
) );
print_r
(
arr_search
(
$data
,
"name=='john'"
) );
print_r
(
arr_search
(
$data
,
"age>25 and name=='john'"
) );
?>
-- results --
Array
(
[0] => Array
(
[name] => bill
[age] => 40
)
[1] => Array
(
[name] => john
[age] => 30
)
[2] => Array
(
[name] => jack
[age] => 50
)
)
Array
(
[0] => Array
(
[name] => john
[age] => 30
)
[1] => Array
(
[name] => john
[age] => 25
)
)
Array
(
[0] => Array
(
[name] => john
[age] => 30
)
)