Last active
January 22, 2016 04:09
-
-
Save huzemin/374f90ce1aacfa6d6ce8 to your computer and use it in GitHub Desktop.
a fast way to get array value by key.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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