Home > php教程 > PHP源码 > php 数组查询

php 数组查询

WBOY
Release: 2016-06-08 17:29:06
Original
1148 people have browsed it
<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;
  }
 }

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template