Array
(
[0] => Array
(
[name] => A
[id] => 1
[phone] => 416-23-55
[Base] => Array
(
[city] => toronto
)
[EBase] => Array
(
[city] => North York
)
[Qty] => 1
)
(
[1] => Array
(
[name] => A
[id] => 1
[phone] => 416-53-66
[Base] => Array
(
[city] => qing
)
[EBase] => Array
(
[city] => chong
)
[Qty] => 2
)
)
如何获取格式为“0,name,id,phone,Base,city,Ebase,Qty”的所有键值?
谢谢!
答案 0 :(得分:14)
试试这个
function array_keys_multi(array $array)
{
$keys = array();
foreach ($array as $key => $value) {
$keys[] = $key;
if (is_array($value)) {
$keys = array_merge($keys, array_keys_multi($value));
}
}
return $keys;
}
答案 1 :(得分:6)
如果您不知道数组的大小是什么,请使用带有foreach循环的递归函数,如果每个$ val是一个数组,则调用自身。如果您确实知道尺寸,那么只需预览每个尺寸并记录每个尺寸的按键。
这样的事情:
<?php
function getKeysMultidimensional(array $array)
{
$keys = array();
foreach($array as $key => $value)
{
$keys[] = $key;
if( is_array($value) ) {
$keys = array_merge($keys, getKeysMultidimensional($value));
}
}
return $keys;
}