Evaluate the input array parameter fields
Determine the fields of input array parameters. A, B, and C are mandatory fields, D, E, and F are possible fields, and others are certain fields that are not allowed.
-
- /**
- * QUESTION: determines the fields of the input array parameter $ params.
- *
- * 1. A, B, and C are required fields
- * 2. D, E, and F are possible fields
- * 3. other fields that are not allowed
- *
- * @ Author yearnfar
- */
- // Method 1:
- $ Must = array ('A', 'B', 'C ');
- $ Maybe = array ('D', 'e', 'F ');
- Foreach ($ must as $ key ){
- If (! Isset ($ params [$ key]) exit ("{$ key} is required! ");
- }
- Foreach ($ params as $ key => $ value ){
- If (! In_array ($ key, $ must) & in_array ($ key, $ maybe )){
- Invalid exit ("{$ key! ");
- }
- }
- // Method 2:
- $ Fields = array ('a' => 1, 'B' => 1, 'C' => 1,
- 'D' => 0, 'E' => 0, 'F' => 0 );
- Foreach ($ params as $ key => $ value ){
- If (! Isset ($ fields [$ key]) {
- Invalid exit ("{$ key! ");
- } Elseif ($ fields [$ key]> 0 ){
- $ Fields [$ key] = 0;
- }
- }
- If (array_sum ($ fields)> 0) {// or if (max ($ fields)> 0)
- Exit ("required fields are missing ");
- }
- // Method 3:
- $ Fields = array ();
- Foreach ($ params as $ key => $ value ){
- Switch ($ key ){
- Case 'a ':
- Case 'B ':
- Case 'C ':
- $ Fields [$ key] = 0;
- Break;
- Case 'D ':
- Case 'e ':
- Case 'F ':
- Break;
- Default:
- Invalid exit ("{$ key! ");
- }
- }
- If (count ($ fields )! = 3 ){
- Exit ("required fields are missing ");
- }
|