fork(1) download
  1. <?php
  2.  
  3. class VerifyInArray
  4. {
  5. public function getMyCollection($field, $collection)
  6. {
  7. $list = array();
  8. if (count($collection)) {
  9. foreach ($collection as $k => $val) {
  10. $list[] = $val[$field];
  11. }
  12. }
  13. return $list;
  14. }
  15.  
  16. public function inMyArray($collection, $field, $findValue)
  17. {
  18. if (isset($collection[0])) {
  19. if (array_key_exists($field, $collection[0]) == false) {
  20. return 'no';
  21. }
  22. }
  23.  
  24. if (in_array($findValue, $this->getMyCollection($field, $collection))) {
  25. return 'ok';
  26. }
  27. return 'no';
  28. }
  29.  
  30. public function displayInArray($collection, $attr, $value)
  31. {
  32. return 'search result: '. $this->inMyArray($collection, $attr, $value);
  33. }
  34.  
  35. }
  36. $src = new VerifyInArray();
  37.  
  38. $collection = array(
  39. 'ID' => 1,
  40. 'name' => 'Smith'
  41. ),
  42. 'ID' => 2,
  43. 'name' => 'John'
  44. )
  45. );
  46. echo $src->displayInArray($collection, 'ID', 2). "\n<br>" .
  47. $src->displayInArray($collection, 'ID', 0);
Success #stdin #stdout 0s 82880KB
stdin
Standard input is empty
stdout
search result: ok
<br>search result: no