获取多维关联数组中的数组计数

时间:2012-06-03 03:39:47

标签: php count associative-array multidimensional-array

我正在尝试确定多维关联数组中每个数组的计数。例如,如果我有一个像:

这样的数组
Array
(
    [food] => Array
        (
            [soup] => Array
                (
                    [chicken noodle] => 
                    [tomato] => 
                    [french onion] => 
                )

            [salad] => Array
                (
                    [house] => 
                    [ceasar] =>
                )

        )
    [drink] => Array
        (
            [soda] => Array
                (
                    [coke] => 
                    [sprite] => 
                    [dr pepper] => 
                )

            [alcoholic] => Array
                (
                    [whiskey] => Array
                        (
                            [Jim Beam] => 
                            [Jameson] =>
                            [Bushmills] =>
                        )
                    [vodka] => Array
                        (
                            [Stolichnaya] => 
                            [Ketel One] =>
                            [Grey Goose] =>
                            [Belvedere] =>
                        )
                    [rum] => Array
                        (
                            [Captain Morgan] => 
                            [Bacardi] =>
                        )
                )
        )
)

不太清楚如何解释这个,所以我会尽我所能。我想找出数组中每个数组的计数。因此,我希望从这个数组中得到的值看起来像:

Array
(
        // total count of all arrays at  "level 1" (i.e. food & drink)
        [0] => 2
        // total count of all arrays at  "level 2" (i.e. soup, salad, soda, alcoholic)
        [1] => 4
        // total count of all arrays at  "level 3" (i.e. chicken noodle, tomato, french onion, house, ceasar, coke, sprite, dr pepper, whiskey, vodka, rum)
        [2] => 11
        // total count of all arrays at  "level 4" (i.e. Jim Beam, Jameson, Bushmills, Stolichnaya, Ketel One, Grey Goose, Belvedere, Captain Morgan, Bacardi)
        [3] => 9
)

现在我知道我只使用值的索引键而不是值,而如果数组是标准的索引数组,那么获取这些值会更容易(我想我是在这里使用正确的术语,随意纠正我 - 当然,我要理解这一点 - 不是因为某人只是“给我一个答案”:))因为我可以在{{{ 1}}循环并相应地增加我的计数。我已经搜索了SO这样的问题,但是没有找到它,如果我错过了它虽然可以随意简单地指出它。谢谢!

解决方案!(阅读会话流程的评论)

for($i = 0 $i < count($array); $i++)

2 个答案:

答案 0 :(得分:1)

更新#2。很多信息,如果我失去你,请告诉我。

我在最后一段代码中犯了一些小错误(需要通过$current + 1代替$current++,而$levels[$current]++位于错误的位置)。我用你的示例数据测试了这个更新;它会对你有用。基本上,calc()是一个递归函数,它遍历数组的每个级别,计算作为数组的每个索引,并将该计数添加到主“计数”数组($levels)。你可以坚持到你的课堂,正如我在下面所做的那样。

它不会故意返回任何内容。 $levelspassed by reference,每个级别的计数都会直接更新。因此,传递给calc()的第一个变量将变为数组,就像您在问题中寻找的那样。

如果您的类脚本看起来与此类似,那么应该收到错误。在class someName {之后,您应该只定义函数和变量。实际执行操作的代码(例如使用echo或设置$test = new Test();)需要在类定义之后放置

话虽如此,如果你想在每次启动这个类时执行一些代码,你应该将这段代码放在类constructor中。

以下是我认为你希望你的班级看起来像。如果您希望$ data在类中,请使用var $data声明变量并将其填充到构造函数中。如果您感到困惑,请告诉我,我可以再次更新我的答案。

// Your class
class Test
{
    // Counts the number of arrays in each level of $data
    function calc(&$levels, $current, $parent)
    {
        if (!isset($levels[$current])) {
            $levels[$current] = 0;
        }
        foreach($parent as $child) {
            $levels[$current]++;
            if (is_array($child)) {
                $this->calc($levels, $current + 1, $child);
            }
        }
    }
}

// Let's use the class! Create an instance of it
$test = new Test();

// Fill the data array
$data = array();
$data['food']['soup']['chicken_noodle'] = '';
$data['food']['soup']['tomato'] = '';
$data['food']['soup']['french onion'] = '';
$data['food']['salad']['house'] = '';
$data['food']['salad']['ceasar'] = '';
$data['drink']['soda']['coke'] = '';
$data['drink']['soda']['sprite'] = '';
$data['drink']['soda']['dr pepper'] = '';
$data['drink']['alcoholic']['whiskey']['Jim Beam'] = '';
$data['drink']['alcoholic']['whiskey']['Jameson'] = '';
$data['drink']['alcoholic']['whiskey']['Bushmills'] = '';
$data['drink']['alcoholic']['vodka']['Stolichnaya'] = '';
$data['drink']['alcoholic']['vodka']['Ketel One'] = '';
$data['drink']['alcoholic']['vodka']['Grey Goose'] = '';
$data['drink']['alcoholic']['vodka']['Belvedere'] = '';
$data['drink']['alcoholic']['rum']['Captain Morgan'] = '';
$data['drink']['alcoholic']['rum']['Bacardi'] = '';    

// Count the data for arrays on each level
$count = array();
$test->calc($count, 0, $data);

// Print the results
echo '<pre>';
print_r($count);
exit ('</pre>');

答案 1 :(得分:0)

如果要获取所有数组中所有元素的总和,可以使用

count($arr, COUNT_RECURSIVE);

Try it online