数组搜索函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数组搜索函数相关的知识,希望对你有一定的参考价值。

Functions to search from arrays as well as recursive arrays.
  1. // search from array
  2. function array_query($array,$what){
  3. if(in_array($what, $array)){
  4. return $array[array_search($what, $array)];
  5. }
  6. return false;
  7. }
  8.  
  9.  
  10. // search from recursive arrays
  11. function recursiveArraySearch($haystack, $needle, $index = null)
  12. {
  13. $aIt = new RecursiveArrayIterator($haystack);
  14. $it = new RecursiveIteratorIterator($aIt);
  15.  
  16. while($it->valid())
  17. {
  18. if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) {
  19. return $aIt->key();
  20. }
  21.  
  22. $it->next();
  23. }
  24.  
  25. return false;
  26. }

以上是关于数组搜索函数的主要内容,如果未能解决你的问题,请参考以下文章