Last active
August 29, 2015 14:02
-
-
Save tondol/8d767a2541bf39451a83 to your computer and use it in GitHub Desktop.
PHPのNoticeを消すのに便利な奴
This file contains hidden or 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
// 第1引数に配列もしくはオブジェクトを, | |
// 第2引数以降に配列のキー名もしくはプロパティ名を与える | |
// 指定した要素やプロパティが存在しなければNULLが返却される | |
function array_at() | |
{ | |
$numargs = func_num_args(); | |
$args = func_get_args(); | |
$arr = array_shift($args); | |
foreach ($args as $index) { | |
if (is_array($arr)) { | |
$arr = isset($arr[$index]) ? $arr[$index] : null; | |
} else if (is_object($arr)) { | |
$arr = isset($arr->{$index}) ? $arr->{$index} : null; | |
} | |
} | |
return $arr; | |
} | |
// nullの代わりにデフォルト値を与えられるようにしたもの | |
// 引数の最後にデフォルト値を与える | |
function array_at_default() | |
{ | |
$numargs = func_num_args(); | |
$args = func_get_args(); | |
$arr = array_shift($args); | |
$default = array_pop($args); | |
foreach ($args as $index) { | |
if (is_array($arr)) { | |
$arr = isset($arr[$index]) ? $arr[$index] : $default; | |
} else if (is_object($arr)) { | |
$arr = isset($arr->{$index}) ? $arr->{$index} : $default; | |
} | |
} | |
return $arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使い方