【问题标题】:Tracking depth in recursion递归跟踪深度
【发布时间】:2010-10-25 08:26:10
【问题描述】:

如果不清楚,请耐心等待;我很难完全解决这个问题(因此我在这里寻求帮助)。

我有一个如下所示的数组:

Array
(
[DimA1] => Array
    (
        [DimB1] => Array
            (
                [DimC1] => Array
                    (
                        [value1] => 13708
                        [value2] => 4.5
                    )

                [DimC2] => Array
                    (
                        [value1] => 1846
                        [value2] => 15.8
                    )

            )

        [DimB2] => Array
            (
                [DimC1] => Array
                    (
                        [value1] => 18166
                        [value2] => 6.4
                    )
            )
[DimA2] => Array
    (
        ....... etc

我需要遍历这个数组,当我到达 value1 和 value2 时,我需要做一些数据库插入。在这个时间点上,我需要知道我当前正在单步执行哪些数组,并将它们的键名用作我的数据库插入的一部分。

我目前的解决方案是这样的:

public function recurseCounts($array,$dims = array()) {
    foreach ($array as $key => $value) {
        $dims[] = $key;
        if (isset($value['value1']) || isset($value['value2'])) {
            print_r($value); // For debugging...
            print_r($dims); // For debugging...
                            // DB Logic to insert dimensions in to DB here
                            // DB Logic to insert values in to DB here
            array_pop($dims);
        } else {
            $this->recurseCounts($value,$dims);
        }
    }
}

这一直有效,直到循环到达 DimB2,这就是事情开始变得古怪的地方。

关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: php recursion


    【解决方案1】:

    您需要跟踪元素的完整路径,因为深度“C”的键是相同的:

    public function recurseCounts($array,$dims = array(),$path = '') {
            foreach ($array as $key => $value) {
                    $dims[] = ($path ? $Path.'_' : '').$key; // Add the full path (separated by '_')
                    if (isset($value['value1']) || isset($value['value2'])) {
                            print_r($value); // For debugging...
                            print_r($dims); // For debugging...
                                // DB Logic to insert dimensions in to DB here
                                // DB Logic to insert values in to DB here
                            array_pop($dims);
                    } else {
                            $this->recurseCounts($value,$dims,end($depth)); // pass it on
                    }
            }
    }
    

    【讨论】:

      【解决方案2】:

      你总是添加到 $dims。

      $dims 现在会消失:

      DimA1
      DimA1, DimB1
      DimA1, DimB1, DimC1
      DimA1, DimB1, DimC2
      DimA1, DimB1, DimB2
      DimA1, DimB1, DimB2, DimC1
      DimA1, DimB1, DimB2, DimC2
      

      如果你把 array_pop 移到外面应该没问题。

      public function recurseCounts($array,$dims = array()) {
          foreach ($array as $key => $value) {
                  $dims[] = $key;
                  if (isset($value['value1']) || isset($value['value2'])) {
                          print_r($value); // For debugging...
                          print_r($dims); // For debugging...
                              // DB Logic to insert dimensions in to DB here
                              // DB Logic to insert values in to DB here
                  } else {
                          $this->recurseCounts($value,$dims);
                  }
                  array_pop($dims);
          }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-12
        • 2012-03-12
        • 2013-08-02
        • 2018-04-17
        • 2016-04-13
        • 2010-11-16
        • 2022-01-23
        相关资源
        最近更新 更多