计算数组中数字键的数量

时间:2016-01-13 14:45:05

标签: php arrays

我使用的是PHP 5.6,我有以下数组:

Array (
    [0] => Array (
            [id] => 1
            [name] => James
        )
    [1] => Array (
            [id] => 2
            [name] => Tim
            [children] => Array (
                    [0] => Array (
                            [id] => 4
                            [name] => Sam
                        )
                    [1] => Array (
                            [id] => 5
                            [name] => Florence
                        )
                )
        )
    [2] => Array (
            [id] => 3
            [name] => Stephen
        )
)

我正在尝试找到一种计算人数的简洁方法,这与计算数字键的数量相同,这应该是5

echo count($myarray); // 3 (only first level)
echo count($myarray, COUNT_RECURSIVE); // 16 (every key/value)

有没有一种很好的方法可以使用内置的PHP函数来完成这项工作,或者我是否需要遍历整个多维数组并手动计算它们??

编辑我的阵列可能最终成为1,000多人(或更多),有许多级别(级别未知)。

2 个答案:

答案 0 :(得分:2)

重要的是要注意,即使内部存在PHP内置(例如count($myarray, COUNT_RECURSIVE_NUMERIC);),它仍然会以递归方式遍历整个数组。如果您担心Out Of Memory错误,请尝试pass-by-reference,它不会复制数组或数组项:

define('COUNT_RECURSIVE', 1);

function count_numeric_keys(&$array, $flags = 0) {
    $count = 0;
    foreach ($array as $key => $value) {
        $count += (int) (is_numeric($key));
        if ($flags & COUNT_RECURSIVE && is_array($value)) {
            $count += count_numeric_keys($value, $flags);
        }
    }
    return (int) $count;
}

$count = count_numeric_keys($array, COUNT_RECURSIVE);

Mayhaps?

与非传递引用,类型提示和小基准的比较:

define('COUNT_RECURSIVE', 1);

function count_numeric_keys(Array &$array, $flags = 0) {
    $count = 0;
    foreach ($array as $key => $value) {
        $count += (int) (is_numeric($key));
        if ($flags & COUNT_RECURSIVE && is_array($value)) {
            $count += count_numeric_keys($value, $flags);
        }
    }
    return (int) $count;
}

function count_numeric_keys_np(Array $array, $flags = 0) {
    $count = 0;
    foreach ($array as $key => $value) {
        $count += (int) (is_numeric($key));
        if ($flags & COUNT_RECURSIVE && is_array($value)) {
            $count += count_numeric_keys_np($value, $flags);
        }
    }
    return (int) $count;
}

$tpl_array = array(
    1=>"one",
    "two"=>"two",
    3=>array(
        1=>1,
        "two"=>2
    )
);

// Fill the array with both numeric and non-numeric
$array = array();
for($i = 1000; $i > 0; $i--) {
    $array[] = $tpl_array;
}

for($i = 1000; $i > 0; $i--) {
    $array["not a number $i"] = $tpl_array;
}

echo "Pre Memory: ".memory_get_usage(TRUE).PHP_EOL;
echo "Count: ".count_numeric_keys($array, COUNT_RECURSIVE).PHP_EOL;
echo "Reference Memory: ".memory_get_usage(TRUE)." current, ".memory_get_peak_usage(TRUE)." peak.\n";

count_numeric_keys_np($array, COUNT_RECURSIVE);
echo "No-Reference Memory: ".memory_get_usage(TRUE)." current, ".memory_get_peak_usage(TRUE)." peak.\n";

IDEONE here上查看。

奇怪的是,在$value上有一个引用,就像foreach($array as $key => &value) {}实际上增加了内存使用量。奇异...

答案 1 :(得分:1)

我刚刚为ya创建了这个递归函数:P

ps -o rss= -p 3432

缩短代码,使其使用三元运算符而不是function countNumericKeys($array) { $count = 0; foreach ($array as $key => $value) { if (is_numeric($key)) { $count ++; } if (is_array($value)) { $count += countNumericKeys($value); } } return $count; } // Test it! $array = [ 1=>"one", "two"=>"two", 3=>[ 1=>1, "two"=>2 ] ]; print countNumericKeys($array); // Output: 3, correct (in this case at least)! s:P

if

使用function simpleCountNumericKeys($array) { $count = 0; foreach ($array as $key => $value) { $count += is_numeric($key) ? 1 : 0; $count += is_array($value) ? simpleCountNumericKeys($value) : 0; } return $count; } 测试 - 只获取数组的顶级键

array_keys

打印2 ...... :(