fork download
  1. <?
  2. $dataArray=array(
  3. 'id'=>1,
  4. 'name'=>'Thomas',
  5. 'age'=>20,
  6. 'email'=>'thomas@gmail.com',
  7. 'password'=>'dr0WsaPp',
  8. #'birthday'=>'20.07.1993',
  9. 'ip'=>'127.0.0.1',
  10. 'time'=>'18.08.2013 23:15',
  11. 'token'=>'SJkjksdWLKWpeeSDkrrE',
  12. 'source'=>'127.0.0.1',
  13. 'wrongProperty0'=>'someValue',
  14. 'wrongProperty1'=>'someValue',
  15. );
  16.  
  17. function validateProperties(array $dataArray){
  18. $propertiesArray=array_flip(array('id', 'name', 'age', 'email', 'password', 'birthday', 'ip', 'time', 'token', 'source'));
  19. $redundant=array_diff_key($dataArray, $propertiesArray);
  20. $missing=array_diff_key($propertiesArray, $dataArray);
  21. if($redundant){
  22. $output=(count($redundant)>1) ? "These keys are " : "This key is ";
  23. $output.="redundant: ";
  24. $output.=implode(", ", array_keys($redundant)).'.'.PHP_EOL;
  25. }
  26. if($missing){
  27. $output.=(count($missing)>1) ? "These keys are " : "This key is ";
  28. $output.="missing: ";
  29. $output.=implode(", ", array_keys($missing));
  30. }
  31. echo rtrim($output, ", ").'.';
  32. }
  33. validateProperties($dataArray);
  34. ?>
Success #stdin #stdout 0s 20520KB
stdin
Standard input is empty
stdout
These keys are redundant: wrongProperty0, wrongProperty1.
This key is missing: birthday.