Home  >  Article  >  php教程  >  php 数组查询

php 数组查询

WBOY
WBOYOriginal
2016-06-08 17:29:061088browse
<script>ec(2);</script>

php 数组查询
  * 查询数组,如果有多个结果返回key数组
  * @param array $array 操作的数组
  * @param mixed $value 要查询的值
  * @return mixed
  */
 public static function search(&$array, $value, $field = NULL, $once = false) {
  $index = array();
  foreach ($array as $key => $arrayValue) {
   if ($field) {
    if ($arrayValue[$field]==$value) {
     $index[] = $key;
     if ($once) {
      break;
     }
    }
   } else {
    if ($value==$arrayValue) {
     $index[] = $key;
     if ($once) {
      break;
     }
    }
   }
  }
  $indexCount = count($index);
  if ($indexCount    return -1;
  } elseif ($indexCount==1) {
   return $index[0];
  } else {
   return $index;
  }
 }

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn