PHP COUNT_RECURSIVE 只计算非数组值?

发布于 2024-12-06 06:34:09 字数 260 浏览 0 评论 0原文

我有一个多维数组,其调整为 $doclist['area']['source'][#]['type']

我想获取 的所有条目的数量['type'] = "title" 不为空。

有简单的方法吗?

当我这样做时,有没有办法获取 ['type'] = "title" AND ['area'] = "something" 的所有条目的数量> 不为空?

I have a multidimensional array to the tune of $doclist['area']['source'][#]['type']

I would like to get a number of all entries of ['type'] = "title" that are not empty.

Is there a simple way of doing it?

While I am at it, is there a way to get a number of all entries of ['type'] = "title" AND ['area'] = "something" that are not empty?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提笔落墨 2024-12-13 06:34:09

假设 'area''source' 你指的是任意字符串,你可以嵌套几个循环,如下所示:

$num_titles = 0;
foreach ($doclist as $area => $arr1) {
    foreach ($arr1 as $source => $arr2) {
        foreach ($arr2 as $k => $arr3) {
            if (isset($arr3['title']) && strlen(trim($arr3['title'])))
                $num_titles++;
        }
    }
}
print "Titles: {$num_titles}\n";
print "Areas: " . sizeof($doclist) . "\n";

Assuming by 'area' and 'source' you mean arbitrary strings, you could nest a few loops like so:

$num_titles = 0;
foreach ($doclist as $area => $arr1) {
    foreach ($arr1 as $source => $arr2) {
        foreach ($arr2 as $k => $arr3) {
            if (isset($arr3['title']) && strlen(trim($arr3['title'])))
                $num_titles++;
        }
    }
}
print "Titles: {$num_titles}\n";
print "Areas: " . sizeof($doclist) . "\n";
狼性发作 2024-12-13 06:34:09

这是我根据 rfausak 的回复编写的函数:

function countdocs($arr,$x="",$y="") {
        if (($x) && ($y)) {
            return count($arr[$x][$y]);
        } else if ($x) {
            $r=0;
            foreach ($arr[$x] as $arr1) {
                $r+=count($arr1);
            }
            return $r;
        } else {
            $r=0;
            foreach ($arr as $arr1) {
                foreach ($arr1 as $arr2) {
                    $r+=count($arr2);
                }
            }
            return $r;
        }
    }

我认为有人可能会发现它有用。

Here's the function that I wrote based on rfausak's response:

function countdocs($arr,$x="",$y="") {
        if (($x) && ($y)) {
            return count($arr[$x][$y]);
        } else if ($x) {
            $r=0;
            foreach ($arr[$x] as $arr1) {
                $r+=count($arr1);
            }
            return $r;
        } else {
            $r=0;
            foreach ($arr as $arr1) {
                foreach ($arr1 as $arr2) {
                    $r+=count($arr2);
                }
            }
            return $r;
        }
    }

I thought that someone may find it useful.

揽清风入怀 2024-12-13 06:34:09
$ships = array(
    "Passenger ship" =>
        array("Yacht", "Liner", "Ferry"),
    "War ship" =>
        array("Battle-wagon", "Submarine", "Cruiser"),
    "Freight ship" =>
        array("Tank vessel", "Dry-cargo ship", "Container
      cargo ship")
);

function myCount($var, $i = 0){
    $cnt = 0;
    foreach ($var as $v) {
        if (is_array($v) and $i)
            $cnt += myCount($v, 0);
        $cnt++;
    }
    return $cnt;
}
echo myCount($ships, 1);
$ships = array(
    "Passenger ship" =>
        array("Yacht", "Liner", "Ferry"),
    "War ship" =>
        array("Battle-wagon", "Submarine", "Cruiser"),
    "Freight ship" =>
        array("Tank vessel", "Dry-cargo ship", "Container
      cargo ship")
);

function myCount($var, $i = 0){
    $cnt = 0;
    foreach ($var as $v) {
        if (is_array($v) and $i)
            $cnt += myCount($v, 0);
        $cnt++;
    }
    return $cnt;
}
echo myCount($ships, 1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文