Guest User

Untitled

a guest
Jul 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. You can use function inArray below, to search:
  2. - multidimensional arrays
  3. - for substrings (case [in]sensitive)
  4. - for sub-arrays
  5. - get array of keys of found values
  6. <?php
  7. /** Function 'inArray' checks if a value exists in an array (also multidimensional): if string as substring (case-[in]sensitive), or other value as a element of the same type
  8. *
  9. * @param mixed $needle Value to find (string: as (sub)string, array: as (sub)array, other: as exact element)
  10. * @param array $haystack The array for search (can be multidimensional)
  11. * @param bool $also_similar (optional, default: false)
  12. * for string $needle - default: false, means case-sensitive,
  13. * 1: case-insensitivity comparision
  14. * 2: case-insensitivity comparision, and also any element of $haystack can be substring of $needle
  15. * for array $needle - default: $needle has to be exact element of $haystack
  16. * 1: array $needle also can have the same values as values of any $haystack element (regardless of order and keys!)
  17. * 2: array $needle values also can be sub-array of any element of $haystack (multidimensional)
  18. * for others - doesn't matter
  19. * @param bool $return_keys (optional, default: false) If true, returns array of keys of found values
  20. * @return bool Whether $needle is found in the array, or keys for found values (first dimension only)
  21. */
  22.  
  23.  
  24.  
  25. function inArray($needle, $haystack, $also_similar = false, $return_keys = false) {
  26.  
  27. if (!is_array($haystack)) return false;
  28. if ($needle===$haystack) return true;
  29. if ($also_similar!==false) if ($also_similar<3) {
  30. if (is_array($needle)) {
  31.  
  32. if (in_array($n=$needle, $h=$haystack, true)) {if ($return_keys) $res[]=$key; else return true;}
  33. elseif ($also_similar>0) {
  34. sort($n);
  35. sort($h);
  36. if (($n===$h) || (in_array($n, $h, true))) {if ($return_keys) $res[]=$key; else return true;}
  37. elseif ($also_similar>1) {
  38. $h = array_intersect($h, $n);
  39. sort($h);
  40. if ($n===$h) {if ($return_keys) $res[]=$key; else return true;}
  41. }
  42. }
  43. }
  44. } else $also_similar-=10;
  45.  
  46. foreach ($haystack as $key => $value) {
  47. if ($needle===$value) {if ($return_keys) $res[]=$key; else return true;}
  48. elseif ((is_string($needle)) && (is_string($value))) {
  49. if (
  50. (strpos($value, $needle)!==false)
  51. || ($also_similar==1) && (stripos($value, $needle)!==false)
  52. || ($also_similar==2) && (stripos($needle, $value)!==false)
  53. ) {
  54. if ($return_keys) $res[]=$key; else return true;
  55. }
  56. } elseif (is_array($value)) {
  57. if(is_array($needle)) {
  58. if (in_array($needle, $value, true)) {if ($return_keys) $res[]=$key; else return true;}
  59. elseif ($also_similar>0) {
  60. $n = $needle;
  61. sort($n);
  62. $h = $value;
  63. sort($h);
  64. if (($n===$h) || (in_array($n, $h, true))) {if ($return_keys) $res[]=$key; else return true;}
  65. elseif ($also_similar>1) {
  66. $h = array_intersect($h, $n);
  67. sort($h);
  68. if ($n===$h) {if ($return_keys) $res[]=$key; else return true;}
  69. }
  70. }
  71. }
  72. if (inArray($needle, $value, $also_similar+10, $return_keys)) {if ($return_keys) $res[]=$key; else return true;}
  73. }
  74. }
  75.  
  76. if (($return_keys)&&(is_array($res))) return $res; else return false;
  77.  
  78. }
  79.  
  80. // Examples
  81.  
  82.  
  83. $a=array (
  84. 1 => 'zero',
  85. 0 => 'zero1',
  86. 2 =>
  87. array (
  88. 'X' => 'AAZery',
  89. 'Y' => 'Zero',
  90. 'Z' => 1234,
  91. 0 =>
  92. array (
  93. 1 => 12345,
  94. 2 => 'ZEro2',
  95. 5 => 'Hello',
  96. ),
  97. ),
  98. 'end' => '123',
  99. )
  100.  
  101. var_dump(inArray(123,$a)); //bool(false)
  102. var_dump(inArray(1234,$a)); //bool(true)
  103. var_export(inArray(1234,$a,0,TRUE)); //array ( 0 => 2, )
  104.  
  105. var_dump(inArray("1234",$a)); //bool(false)
  106. var_dump(inArray("23",$a)); //bool(true)
  107. var_export(inArray("23",$a,0,TRUE)); //array ( 0 => 'end', )
  108.  
  109. var_dump(inArray(12345,$a)); //bool(true)
  110.  
  111. var_dump(inArray("zer",$a)); //bool(true)
  112. var_export(inArray("zer",$a,0,TRUE)); /* array (
  113. 0 => 1,
  114. 1 => 0,
  115. ) */
  116.  
  117. var_dump(inArray("Zer",$a)); //bool(true)
  118. var_dump(inArray("ZER",$a)); //bool(false)
  119.  
  120. var_dump(inArray("ZER",$a,1)); //bool(true)
  121. var_export(inArray("ZER",$a,1,TRUE)); /* array (
  122. 0 => 1,
  123. 1 => 0,
  124. 2 => 2,
  125. ) */
  126.  
  127. var_dump(inArray("ZERo2",$a,1)); //bool(true)
  128. var_dump(inArray("ZERo2",$a)); //bool(false)
  129.  
  130. var_dump(inArray(array(1=>12345,2=>"ZEro2"),$a)); //bool(false)
  131. var_dump(inArray(array(2=>"ZEro2",1=>12345,"Hello"),$a)); //bool(false)
  132.  
  133. var_dump(inArray(array(2=>"ZEro2",1=>12345,"Hello"),$a,1)); //bool(true)
  134. var_export(inArray(array(2=>"ZEro2",1=>12345,"Hello"),$a,1,TRUE)); //array ( 0 => 2,)
  135.  
  136. var_dump(inArray(array(2=>"ZEro2",1=>12345),$a,2)); //bool(true)
  137. var_export(inArray(array(2=>"ZEro2",1=>12345),$a,2,TRUE)); //array ( 0 => 2,)
  138.  
  139. var_dump(inArray(array(2=>"ZEro2",1=>12345,"Hello"),$a,3)); //bool(false)
  140.  
  141.  
  142. ?>
Add Comment
Please, Sign In to add comment