通用的方法,来检查字段是否存在

 1 class AlarmController
 2 {    
 3     const VALID = 1;  //有效
 4     const INVALID = 0;  //无效
 5     
 6      /**
 7      * @param array $data
 8      * @return bool
 9      */
10     public function doCheckShareholderApproval( array $data )
11     {
12         $fields = array( 'initiator', 'role', 'uid' );
13         $check = $this->verifyInputParams( $data, $fields );
14 
15         if ( $check['status'] == self::INVALID ) {
16             $this->error = array(
17                 'errorCode' => 0,
18                 'errorMsg' => $check['msg']
19             );
20             return false;
21         }
22     }
23     
24     /**
25      * Check whether the field exists, there is a return value, there is no return error information
26      * 检查字段是否存在,存在返回数值,不存在返回错误信息
27      * @param $data
28      * @param $fields
29      * @return array
30      */
31     function verifyInputParams($data, $fields) {
32         $error = array('status' => 1);
33         $param = array();
34         foreach ($fields as $key => $val) {
35             if (!isset($data[$val])) {
36                 $error['status'] = 0;
37                 $error['msg'] = 'Illegal operation not find ' . $val . ' field';
38                 return $error;
39             }
40 
41             $param[$key] = $data[$val];
42         }
43 
44         $error['data'] = $param;
45         return $error;
46     }
47 }
原文地址:https://www.cnblogs.com/spectrelb/p/6803659.html