Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. array(
  2. 0=>"1-2",
  3. 1=>"2-3"
  4. 2=>"3-4"
  5. 3=>"4-3"
  6. )
  7.  
  8. $input = array(
  9. 0 => "1-2",
  10. 1 => "2-3",
  11. 2 => "3-4",
  12. 3 => "4-3",
  13. );
  14.  
  15. $search = '2-';
  16.  
  17. $index = -1;
  18. foreach ($input as $key => $value) {
  19. if (strpos($value, $search) === 0) {
  20. $index = $key;
  21. break;
  22. }
  23. }
  24.  
  25. <?php
  26. $find = '2-';
  27. $array = array(0=>"1-2", 1=>"2-3", 2=>"3-4", 3=>"4-3");
  28. $position = NULL;
  29.  
  30. foreach($array as $key=>$val){
  31. if( substr($val,0,strlen($find)) == $find ){
  32. $position = $key;
  33. break;
  34. }
  35. }
  36.  
  37. echo $position;
  38.  
  39. $needle = '2-';
  40. $result = array_filter(
  41. $myArray,
  42. function($value) use ($needle) {
  43. return fnmatch($needle.'*', $value);
  44. }
  45. );
  46. var_dump($result);
  47.  
  48. $input = array('1-2', '2-3', '3-4', '4-3');
  49. $search = '2-';
  50. var_dump(preg_grep("/^{$search}/", $input));
  51.  
  52. $input = array('1-2', '2-3', '3-4', '4-3');
  53. $search = '2-';
  54.  
  55. $results = array_filter(
  56. $input,
  57. function ($item) use ($search) {
  58. return strpos($item, $search) === 0;
  59. }
  60. );
  61.  
  62. // $results will contain an array of all strings that match.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement