10
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、姓名、身份证、电话、基地、城市、Ebase、数量”的所有键值?

谢谢!

4

2 回答 2

15

试试这个

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;
}
于 2012-06-27T21:17:00.793 回答
5

如果您不知道数组的大小,请使用带有 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;

}
于 2012-06-27T21:15:25.053 回答