//二维数组查询及排序并返回所需字段 //数所表查数组查询$where条件同时满足 function search_2arr(array $array,array $where=[],$field=false,$order=false){ if($order){ //排序 $orderArr = explode(' ',$order); $key = $orderArr[0]; $sort = isset($orderArr[1])?strtoupper($orderArr[1]):'ASC'; $sort = $sort=='ASC'?SORT_ASC :SORT_DESC; $array = self::sort_2arr($array,$key,$sort); } $ret = []; foreach ($array as $val){ $flag = true; foreach ($where as $k=>$v){ if(isset($val[$k])){ if(gettype($v) == 'array'){ switch (strtolower($v[0])){ case 'like': if(strpos($val[$k],$v[1])===false) $flag = false; break; case '=': case 'eq':if($val[$k] != $v[1]) $flag = false; break; case '>': case 'gt':if($val[$k] <= $v[1]) $flag = false; break; case '<': case 'lt':if($val[$k] >= $v[1]) $flag = false; break; case 'neq': case '!=':if($val[$k] == $v[1]) $flag = false; break; case '>=': case 'egt':if($val[$k] < $v[1]) $flag = false; break; case '<=': case 'elt':if($val[$k] > $v[1]) $flag = false; break; case 'in': $condition = ($val[$k] != $v[1][0]); for ($i=1;$i<count($v[1]);$i++){ $condition = $condition && ($val[$k] != $v[1][$i]); } if($condition) $flag = false; break; case 'not in': $condition = ($val[$k] == $v[1][0]); for ($i=1;$i<count($v[1]);$i++){ $condition = $condition || ($val[$k] == $v[1][$i]); } if($condition) $flag = false; break; } }else{ if($val[$k] != $v) $flag = false; } } } if($flag){ if($field){ $arr = []; if(gettype($field) != 'array') $field = explode(',',$field); foreach ($field as $v){ $arr[$v] = $val[$v]; } $ret[] = $arr; }else{ $ret[] = $val; } } } return $ret; } //二维数组排序 function sort_2arr($array,$keys,$sort=SORT_ASC){ $keysValue = []; foreach ($array as $k => $v) { $keysValue[$k] = $v[$keys]; } array_multisort($keysValue, $sort, $array); return $array; }