Evaluate the input array parameter fields

Source: Internet
Author: User
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.

  1. /**
  2. * QUESTION: determines the fields of the input array parameter $ params.
  3. *
  4. * 1. A, B, and C are required fields
  5. * 2. D, E, and F are possible fields
  6. * 3. other fields that are not allowed
  7. *
  8. * @ Author yearnfar
  9. */
  10. // Method 1:
  11. $ Must = array ('A', 'B', 'C ');
  12. $ Maybe = array ('D', 'e', 'F ');
  13. Foreach ($ must as $ key ){
  14. If (! Isset ($ params [$ key]) exit ("{$ key} is required! ");
  15. }
  16. Foreach ($ params as $ key => $ value ){
  17. If (! In_array ($ key, $ must) & in_array ($ key, $ maybe )){
  18. Invalid exit ("{$ key! ");
  19. }
  20. }
  21. // Method 2:
  22. $ Fields = array ('a' => 1, 'B' => 1, 'C' => 1,
  23. 'D' => 0, 'E' => 0, 'F' => 0 );
  24. Foreach ($ params as $ key => $ value ){
  25. If (! Isset ($ fields [$ key]) {
  26. Invalid exit ("{$ key! ");
  27. } Elseif ($ fields [$ key]> 0 ){
  28. $ Fields [$ key] = 0;
  29. }
  30. }
  31. If (array_sum ($ fields)> 0) {// or if (max ($ fields)> 0)
  32. Exit ("required fields are missing ");
  33. }
  34. // Method 3:
  35. $ Fields = array ();
  36. Foreach ($ params as $ key => $ value ){
  37. Switch ($ key ){
  38. Case 'a ':
  39. Case 'B ':
  40. Case 'C ':
  41. $ Fields [$ key] = 0;
  42. Break;
  43. Case 'D ':
  44. Case 'e ':
  45. Case 'F ':
  46. Break;
  47. Default:
  48. Invalid exit ("{$ key! ");
  49. }
  50. }
  51. If (count ($ fields )! = 3 ){
  52. Exit ("required fields are missing ");
  53. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.