// type number,string,array ,email,ip,regexp
$checkArray = array(
'a'=>array(
'required'=>true,
'type' =>'number',
'options' =>'',
'error' =>'',
),
'b'=>array(
'required'=>true,
'type' =>'email',
'options' =>'',
'error' =>'',
),
'c'=>array(
'required'=>false,
'type' =>'regexp',
'options' =>"/^[0-9a-z]{1,5}$/i",
'error' =>'',
),
'd'=>array(
'required'=>true,
'type' =>'email',
'options' =>'',
'error' =>'',
),
);
function checkParam ($checkArray,$paramArray) {
$keys = array_keys($checkArray);
try{
if(!is_array($paramArray))
throw new Exception(' $param is not array');
} catch ( Exception $e) {
echo $e ->getMessage();die;
}
foreach ($paramArray as $key => $value) {
if( in_array($key, $keys ) ) {
if( $checkArray[$key]['required'] ) {
echo switchCheck($checkArray[$key],$value,$key);
echo "\r\n";
} else {
if(!empty($value)) {
echo switchCheck($checkArray[$key],$value,$key);
echo "\r\n";
}
}
}
}
}
function switchCheck ($type , $value,$key) {
if(empty($type)) return false;
switch ($type['type']) {
case 'number':
if(!is_numeric($value))
echo $key.' is not number';
break;
case 'email':
if(!filter_var($value,FILTER_VALIDATE_EMAIL) )
echo $key.' is not email';
break;
case 'array':
if(!is_array($value))
echo $key.' is not array';
break;
case 'regexp':
if(empty($type['options'])) echo $key.' regexp is null';
if(!filter_var($value,FILTER_VALIDATE_REGEXP,array('options'=>array('regexp'=>$type['options'] ) ) ) )
echo $key.' regexp error';
break;
default:
if(!$value)
echo $key.' not null ';
break;
}
}