Created
July 18, 2018 00:18
-
-
Save msegu/80093a65316ded5b69558d5456f80ff9 to your computer and use it in GitHub Desktop.
php in_array() extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You can use function inArray below, to search: | |
- multidimensional arrays | |
- for substrings (case [in]sensitive) | |
- for sub-arrays | |
- get array of keys of found values | |
<?php | |
/** 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 | |
* | |
* @param mixed $needle Value to find (string: as (sub)string, array: as (sub)array, other: as exact element) | |
* @param array $haystack The array for search (can be multidimensional) | |
* @param bool $also_similar (optional, default: false) | |
* for string $needle - default: false, means case-sensitive, | |
* 1: case-insensitivity comparision | |
* 2: case-insensitivity comparision, and also any element of $haystack can be substring of $needle | |
* for array $needle - default: $needle has to be exact element of $haystack | |
* 1: array $needle also can have the same values as values of any $haystack element (regardless of order and keys!) | |
* 2: array $needle values also can be sub-array of any element of $haystack (multidimensional) | |
* for others - doesn't matter | |
* @param bool $return_keys (optional, default: false) If true, returns array of keys of found values | |
* @return bool Whether $needle is found in the array, or keys for found values (first dimension only) | |
*/ | |
function inArray($needle, $haystack, $also_similar = false, $return_keys = false) { | |
if (!is_array($haystack)) return false; | |
if ($needle===$haystack) return true; | |
if ($also_similar!==false) if ($also_similar<3) { | |
if (is_array($needle)) { | |
if (in_array($n=$needle, $h=$haystack, true)) {if ($return_keys) $res[]=$key; else return true;} | |
elseif ($also_similar>0) { | |
sort($n); | |
sort($h); | |
if (($n===$h) || (in_array($n, $h, true))) {if ($return_keys) $res[]=$key; else return true;} | |
elseif ($also_similar>1) { | |
$h = array_intersect($h, $n); | |
sort($h); | |
if ($n===$h) {if ($return_keys) $res[]=$key; else return true;} | |
} | |
} | |
} | |
} else $also_similar-=10; | |
foreach ($haystack as $key => $value) { | |
if ($needle===$value) {if ($return_keys) $res[]=$key; else return true;} | |
elseif ((is_string($needle)) && (is_string($value))) { | |
if ( | |
(strpos($value, $needle)!==false) | |
|| ($also_similar==1) && (stripos($value, $needle)!==false) | |
|| ($also_similar==2) && (stripos($needle, $value)!==false) | |
) { | |
if ($return_keys) $res[]=$key; else return true; | |
} | |
} elseif (is_array($value)) { | |
if(is_array($needle)) { | |
if (in_array($needle, $value, true)) {if ($return_keys) $res[]=$key; else return true;} | |
elseif ($also_similar>0) { | |
$n = $needle; | |
sort($n); | |
$h = $value; | |
sort($h); | |
if (($n===$h) || (in_array($n, $h, true))) {if ($return_keys) $res[]=$key; else return true;} | |
elseif ($also_similar>1) { | |
$h = array_intersect($h, $n); | |
sort($h); | |
if ($n===$h) {if ($return_keys) $res[]=$key; else return true;} | |
} | |
} | |
} | |
if (inArray($needle, $value, $also_similar+10, $return_keys)) {if ($return_keys) $res[]=$key; else return true;} | |
} | |
} | |
if (($return_keys)&&(is_array($res))) return $res; else return false; | |
} | |
// Examples | |
$a=array ( | |
1 => 'zero', | |
0 => 'zero1', | |
2 => | |
array ( | |
'X' => 'AAZery', | |
'Y' => 'Zero', | |
'Z' => 1234, | |
0 => | |
array ( | |
1 => 12345, | |
2 => 'ZEro2', | |
5 => 'Hello', | |
), | |
), | |
'end' => '123', | |
) | |
var_dump(inArray(123,$a)); //bool(false) | |
var_dump(inArray(1234,$a)); //bool(true) | |
var_export(inArray(1234,$a,0,TRUE)); //array ( 0 => 2, ) | |
var_dump(inArray("1234",$a)); //bool(false) | |
var_dump(inArray("23",$a)); //bool(true) | |
var_export(inArray("23",$a,0,TRUE)); //array ( 0 => 'end', ) | |
var_dump(inArray(12345,$a)); //bool(true) | |
var_dump(inArray("zer",$a)); //bool(true) | |
var_export(inArray("zer",$a,0,TRUE)); /* array ( | |
0 => 1, | |
1 => 0, | |
) */ | |
var_dump(inArray("Zer",$a)); //bool(true) | |
var_dump(inArray("ZER",$a)); //bool(false) | |
var_dump(inArray("ZER",$a,1)); //bool(true) | |
var_export(inArray("ZER",$a,1,TRUE)); /* array ( | |
0 => 1, | |
1 => 0, | |
2 => 2, | |
) */ | |
var_dump(inArray("ZERo2",$a,1)); //bool(true) | |
var_dump(inArray("ZERo2",$a)); //bool(false) | |
var_dump(inArray(array(1=>12345,2=>"ZEro2"),$a)); //bool(false) | |
var_dump(inArray(array(2=>"ZEro2",1=>12345,"Hello"),$a)); //bool(false) | |
var_dump(inArray(array(2=>"ZEro2",1=>12345,"Hello"),$a,1)); //bool(true) | |
var_export(inArray(array(2=>"ZEro2",1=>12345,"Hello"),$a,1,TRUE)); //array ( 0 => 2,) | |
var_dump(inArray(array(2=>"ZEro2",1=>12345),$a,2)); //bool(true) | |
var_export(inArray(array(2=>"ZEro2",1=>12345),$a,2,TRUE)); //array ( 0 => 2,) | |
var_dump(inArray(array(2=>"ZEro2",1=>12345,"Hello"),$a,3)); //bool(false) | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment