douduan7295 2017-01-17 15:52
浏览 33

全局访问数组变量的特定索引/键的最佳方法是什么?

I have an array

$user = array(
    "name" => "John Doe",
    "age" => "26",
    "country" => "US",
    "city" => "LA"
);

I want to access the variable's key "age" in a function. I'm currently using this:

global $user;
return $user['age'];

But I wonder if this is more efficient:

$age = $GLOBALS['user']['age'];
return $age;

Which one is best if you want to globally access only specific indexes/keys of an array variable?

  • 写回答

0条回答 默认 最新

    报告相同问题?