這裏有一個功能我用,可以幫助你:
/** todo handle numeric values
* @param array $array The array from which to get the value
* @param array $parents An array of parent keys of the value,
* starting with the outermost key
* @param bool $key_exists If given, an already defined variable
* that is altered by reference
* @return mixed The requested nested value. Possibly NULL if the value
* is NULL or not all nested parent keys exist.
* $key_exists is altered by reference and is a Boolean
* that indicates whether all nested parent keys
* exist (TRUE) or not (FALSE).
* This allows to distinguish between the two
* possibilities when NULL is returned.
*/
function &getValue(array &$array, array $parents, &$key_exists = NULL)
{
$ref = &$array;
foreach ($parents as $parent) {
if (is_array($ref) && array_key_exists($parent, $ref))
$ref = &$ref[$parent];
else {
$key_exists = FALSE;
$null = NULL;
return $null;
}
}
$key_exists = TRUE;
return $ref;
}
它得到,即使這個數組嵌套數組中的元素的值。如果路徑不存在,則返回null。魔法!
例如:
$arr = [
'path' => [
'of' => [
'nestedValue' => 'myValue',
],
],
];
print_r($arr);
echo getValue($arr, array('path', 'of', 'nestedValue'));
var_dump(getValue($arr, array('path', 'of', 'nowhere')));
怎麼樣,如果(空($本 - > _可用[$選項] [ 'accepts_argument'])!){} – bksi 2014-09-23 00:42:13
沒有替代'isset',因爲它是不是功能。它是一種語言結構。如果您嘗試將未定義的變量傳遞給自定義函數,那麼如果通過引用傳遞參數,您將得到一個警告 – 2014-09-23 00:42:22
@true! – 2014-09-23 00:48:51