Home >Backend Development >PHP Tutorial >Simple example of getting Key from PHP array based on value

Simple example of getting Key from PHP array based on value

WBOY
WBOYOriginal
2016-07-25 08:55:411463browse
  1. /**
  2. * Get Key based on the value of php array
  3. * by bbs.it-home.org
  4. */
  5. function getKey($arr, $value) {
  6. if(!is_array($arr)) return null;
  7. foreach($arr as $k =>$v) {
  8. $return = getKey($v, $value);
  9. if($v == $value){
  10. return $k;
  11. }
  12. if(!is_null($return)){
  13. return $return;
  14. }
  15. }
  16. }
复制代码


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