Skip to content

Instantly share code, notes, and snippets.

@huzemin
Last active January 22, 2016 04:09
Show Gist options
  • Save huzemin/374f90ce1aacfa6d6ce8 to your computer and use it in GitHub Desktop.
Save huzemin/374f90ce1aacfa6d6ce8 to your computer and use it in GitHub Desktop.
a fast way to get array value by key.
<?php
/**
* 快速获取数据的值
* @param $soure
* @param $key
* @param string $separetor
* @return array|null
* @throws Exception
*/
function _v($soure, $key, $separetor = '.') {
if(empty($key) || !is_array($soure)) {
return $soure;
}
$_key = explode($separetor, $key);
$nested = 100;
$obj = $soure;
while(true) {
$nested --;
if($nested == 0) {
throw new Exception("数组维数太大了!!!");
}
if(count($_key) > 0) {
$_k = array_shift($_key);
if(isset($obj[$_k])) {
$obj = $obj[$_k];
} else {
return null;
}
} else {
break;
}
}
return $obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment