Skip to content

Instantly share code, notes, and snippets.

@unique1984
Last active February 21, 2019 09:34
Show Gist options
  • Save unique1984/6472eacfbffaf6e8332d69c0d83c0d68 to your computer and use it in GitHub Desktop.
Save unique1984/6472eacfbffaf6e8332d69c0d83c0d68 to your computer and use it in GitHub Desktop.
Recursive Array Search class for PHP
<?php
/*****************************************
*
* array search Copyright (C) 2009 - 2017 Yasin KARABULAK (YsnSoftware) All Rights Reserved
* email: info@yasinkarabulak.com
*
*******************************************/
class arraySearch
{
private $arraySearchResult = array();
//~ start of recursive array_
//~ alias of special array_search
public function my_array_search($needle, $haystack)
{
return $this->my_recursive_array_search($needle, $haystack, false, false);
}
//~ alias of special array_key_exists
private function my_recursive_array_search($needle, $haystack, $in = false, $recursive = false, $before = null, $originalData = array())
{
/* using:
* if [$in = false and $recursive = false] 1 dimention array keys of needle==value, result can be multiple.
* if [$in = true and $recursive = false] it is the same as array_key_exists() but this can return value of that key
* if [$in = false and $recursive = true] it searchs all of values and returns || $array[][key,that,point,of]=$value_needle || formatted comma seperated
* if [$in = true and $recursive = true] it searchs all of keys and returns || $array[key][points][key][needle]=$value; || formatted comma seperated
* $before and $originalData[f] recursive args do not bother it :)
*/
//~ future use, some kind of check mechanism or rarrayKeyToData() will integrate in this function
if (count($originalData) == 0) {
$originalData = $haystack;
}
if (!$in && !$recursive) {
//~
$returnData = array();
foreach ($haystack as $key => $value) {
if ($needle === $value) {
$returnData[] = $key;
}
}
return count($returnData) > 0 ? array_values($returnData) : false;
//~
} elseif ($in && !$recursive) {
//~
return array_key_exists($needle, $haystack) ? $haystack[$needle] : false;
//~
} elseif (!$in && $recursive) {
//~
foreach ($haystack as $key => $value) {
if (is_array($value)) {
$this->my_recursive_array_search($needle, $value, false, true, $before . $key . ",");
} elseif ($needle === $value) {
$this->arraySearchResult[$before . $key] = $value;
}
}
if (count($this->arraySearchResult) > 0) {
return $this->arraySearchResult;
} else {
return false;
}
//~
} elseif ($in && $recursive) {
//~
foreach ($haystack as $key => $value) {
if (is_array($value)) {
if ($needle === $key) {
$this->arraySearchResult[$before . $key] = $value;
}
$this->my_recursive_array_search($needle, $value, true, true, $before . $key . ",");
} elseif ($needle === $key) {
$this->arraySearchResult[$before . $key] = $value;
}
}
if (count($this->arraySearchResult) > 0) {
return $this->arraySearchResult;
} else {
return false;
}
//~
}
}
//~ alias of recursive array_search
public function my_array_key_exists($needle, $haystack)
{
return $this->my_recursive_array_search($needle, $haystack, true, false);
}
//~ alias of recursive array_key_exists
public function rarray_search($needle, $haystack)
{
$this->arraySearchResult = array();
return $this->my_recursive_array_search($needle, $haystack, false, true);
}
public function rarray_key_exists($needle, $haystack)
{
$this->arraySearchResult = array();
return $this->my_recursive_array_search($needle, $haystack, true, true);
}
public function rarrayKeyToData($explodable, $haystack, $arr = false)
{
$value = $haystack;
//~ foreach(explode(",",$explodable) as $k){
//~ echo $arr?$k."\n":null;
//~ $value=$value[$k];
//~ }
return $value[explode(",", $explodable)[0]];
}
//~ end of recursive array_
}
@unique1984
Copy link
Author

unique1984 commented Sep 4, 2017

Using

include "arraySearch.class.php";
$search=new arraySearch();

$search->my_array_search("needle",array());// returns depth 1 matched values in the array as an array.

$search->my_array_key_exists("needle",array());// returns depth 1 matched keys in the array as an array.

$search->rarray_search("needle",array());// returns all matched values in the array as an array.

$search->rarray_key_exists("needle",array());// returns all matched keys in the array as an array.

#regex will be implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment