查找数组数组中的级别数的算法

时间:2015-02-07 13:51:26

标签: php arrays algorithm categories

我正在研究一种算法来计算数组数组中的级别数量。

我需要这个的原因是因为我需要从属于父类别的数据库中获取类别列表,并且根据此数组的级别数量,我需要显示一个类别列表的数量(到选择类别)。

因此,它将是每个类别级别的类别列表,例如

Vehicles
        Cars
            honda
                   Red
                   Blue
                   Yellow
            ford
                   Red
            suzuki
                   Red
                   Green
            BMW
        Motorcycles
            bla bla 
                 bla bla 
Groceries
        Fruits
            Berries
                    Red
                        Strawberries

所以我需要一个函数来检查所选父级的级别数量,例如,如果我将车辆的ID传递给我想要它返回4或3,如果我们将车辆计为0级,那么我知道如果客户从第一个列表中选择Vechicles我将不得不再显示3个列表。

到目前为止,我所拥有的不起作用是

function count_children_level($list_of_children, $start_depth = 0){    

    // if the data being passed is an array
    if(is_array($list_of_children)){

        // amount of nodes is equal to the 
        $max = $start_depth;

        foreach($list_of_children as $i){

            $result = count_children_level($i, $start_depth + 1);

            if ($result > $max){

                $max = $result;
            }
        }
        return $max;
    }
    //if is not array
    else {
        return $start_depth;
    }
}

我真的需要了解这是如何工作的,因为我必须使用像这样的几个函数,所以如果可以的话,请详细解释你的答案。

由于

3 个答案:

答案 0 :(得分:2)

嵌套数组的深度等于其中最大数组的深度+ 1。

因此,对于递归函数,不是每次都传递整个数组,而是可以进行实际的递归调用,只获取子数组的深度。因此,对于普通的平面数组,此函数返回1,每个级别返回1个额外值。

<?php
function array_depth($array) {
  // Determine largest sub-array. Start with 0 if there are no arrays at all.
  $max = 0;
  foreach ($array as $item) {
    if (is_array($item)) {
      // Make the recursive call, passing not $array, but the sub-array ($item)
      // to the function again.
      $depth = array_depth($item);
      if ($depth > $max)
        $max = $depth;
    }
  }
  // Depth of this array is the depth of the largest sub-array + 1.
  return $max + 1;
}

我这样称呼它:

echo array_depth(
  array('x' => 
    array('y' => 
      array('z'))));  // Returns 3.

答案 1 :(得分:2)

我对@GolezTrol在their answer中所说的内容的解释(&#34;嵌套数组的深度等于其中最大数组的深度+ 1&#34; ):

function array_depth($a)
{
    // If $a is not an array or it's an empty array then its depth is 1
    if (! is_array($a) || count($a) == 0) {
        return 0;
    }

    // Otherwise, add 1 to the maximum depth of the elements it contains
    return 1 + max(array_map('array_depth', $a));
}

答案 2 :(得分:0)

RecursiveIteratorIterator类的另一个解决方案。这样您就不需要递归函数了:

$array = array(
    'Vehicles' => array(
        'Cars' => array(
            'honda' => array(
                'Red',
                'Blue',
                'Yellow',
            )
        )
    )
);

function getTotalDepth($array) {
    $iterator = new RecursiveIteratorIterator(
        new RecursiveArrayIterator($array)
    );
    $max = 0;
    foreach ($iterator as $element) {
        if (!$iterator->callHasChildren()) {
            $max = max($max, $iterator->getDepth());
        }
    }
    return $max;
}

echo getTotalDepth($array);

如果要迭代整个数组,也非常有用:

$iterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator($array),
    RecursiveIteratorIterator::SELF_FIRST
);

foreach ($iterator as $element) {
    print_r($element);
    echo '<br>';
}