【发布时间】:2014-01-17 22:58:42
【问题描述】:
我有一个同事。多维数组和一个函数:
// 输入:
$array = array();
$array['keyname0'] = array(
'key0' => 'value0',
'key1' => 'value1',
//etc,
);
$array['keyname1'] = array(
'key0' => 'value0',
'key1' => 'value1',
//etc,
);
// 方法:
function getCurrentParentArrayKey($array){
//should return current key name of this array
//I can't for the life of me find any array function on php.net or anywhere that solves this
}
// 执行:
print getCurrentParentArrayKey($array['keyname0']);
// 输出:
keyname0
一个更好的例子可能是:
$users=array(
'michael' => array(
'age' => '28',
'height' => '5\'9"',
)
);
function getUserName($array){
//do something
//@return: 'michael'
}
print getUserName($users['michael']);
【问题讨论】:
-
您将内部数组发送给示例中的函数,因此函数无法知道它接收到的数组是更大数组、对象等的一部分。它只是一个数组。
-
您将这个键的值包含在函数中,而不是键名
-
你想要你已经传递给函数的密钥?! :o
标签: php arrays multidimensional-array array-key