2009-02-07 122 views
2

無謂的運球PHP陣列,追加的數組項數遞歸到數組


好吧,這是從我的另一個奇怪的一個,我要感謝OIS幫了我對我的最後一個問題......這處理這種同樣時髦的數組操作...我深入研究了代碼,並且我感覺它幫助我通過遞歸數組操作函數變得更好。然而,我再次找回自我的另一項艱鉅的現貨

實際的問題


我想寫一個遞歸函數操縱諸如 this。除了獲取每個數組元素的深度。 它會爲每個數組元素添加一定數量的增量計數。查看我想要做的最簡單的方法是查看「樣本數組」和「期望結果數組」......我覺得我在理解這些遞歸函數方面越來越好。但是這個給了我一個地獄,提前感謝你給我提供的任何幫助。 請忽略示例數組的[depth]結果,我已經有一個函數添加了這個。再次 感謝, - YouDontMeanMuch

樣品陣列


  array (
       52 => 
       array (
       'title' => 'Website Navigation', 
       'path' => '', 
       'type' => '115', 
       'pid' => 0, 
       'hasChildren' => 1, 
       'children' => 
       array (
        53 => 
        array (
        'title' => 'Home', 
        'path' => '', 
        'type' => '118', 
        'pid' => 52, 
        'hasChildren' => 0, 
       ), 
        54 => 
        array (
        'title' => 'Features', 
        'path' => 'features', 
        'type' => '374', 
        'pid' => 52, 
        'hasChildren' => 1, 
        'children' => 
        array (
         59 => 
         array (
         'title' => 'artistic', 
         'path' => 'features/artistic', 
         'type' => '374', 
         'pid' => 54, 
         'hasChildren' => 1, 
         'children' => 
         array (
          63 => 
          array (
          'title' => 'galleries', 
          'path' => 'features/artistic/galleries', 
          'type' => '374', 
          'pid' => 59, 
          'hasChildren' => 1, 
          'children' => 
          array (
           65 => 
           array (
           'title' => 'graphics', 
           'path' => 'features/artistic/galleries/graphics', 
           'type' => '118', 
           'pid' => 63, 
           'hasChildren' => 0, 
          ), 
           67 => 
           array (
           'title' => 'mixed medium', 
           'path' => 'features/artistic/galleries/mixed-medium', 
           'type' => '118', 
           'pid' => 63, 
           'hasChildren' => 0, 
          ), 
           64 => 
           array (
           'title' => 'overview', 
           'path' => 'features/artistic/galleries', 
           'type' => '118', 
           'pid' => 63, 
           'hasChildren' => 0, 
          ), 
           68 => 
           array (
           'title' => 'photography', 
           'path' => 'features/artistic/galleries/photography', 
           'type' => '118', 
           'pid' => 63, 
           'hasChildren' => 0, 
          ), 
           66 => 
           array (
           'title' => 'traditional', 
           'path' => 'features/artistic/galleries/traditional', 
           'type' => '118', 
           'pid' => 63, 
           'hasChildren' => 0, 
          ), 
          ), 
         ), 
          62 => 
          array (
          'title' => 'overview', 
          'path' => 'features/artistic', 
          'type' => '118', 
          'pid' => 59, 
          'hasChildren' => 0, 
         ), 
          69 => 
          array (
          'title' => 'tutorials', 
          'path' => 'features/artistic/tutorials', 
          'type' => '374', 
          'pid' => 59, 
          'hasChildren' => 1, 
          'children' => 
          array (
           71 => 
           array (
           'title' => 'by category', 
           'path' => 'features/artistic/tutorials/by-category/', 
           'type' => '118', 
           'pid' => 69, 
           'hasChildren' => 0, 
          ), 
           72 => 
           array (
           'title' => 'by date', 
           'path' => 'features/artistic/tutorials/by-date/', 
           'type' => '118', 
           'pid' => 69, 
           'hasChildren' => 0, 
          ), 
           70 => 
           array (
           'title' => 'overview', 
           'path' => 'features/artistic/tutorials', 
           'type' => '118', 
           'pid' => 69, 
           'hasChildren' => 0, 
          ), 
          ), 
         ), 
         ), 
        ), 
         58 => 
         array (
         'title' => 'overview', 
         'path' => 'features', 
         'type' => '118', 
         'pid' => 54, 
         'hasChildren' => 0, 
        ), 
         61 => 
         array (
         'title' => 'projects/labs', 
         'path' => 'features/projects-labs/', 
         'type' => '374', 
         'pid' => 54, 
         'hasChildren' => 0, 
        ), 
         60 => 
         array (
         'title' => 'web development', 
         'path' => 'features/web-development', 
         'type' => '374', 
         'pid' => 54, 
         'hasChildren' => 1, 
         'children' => 
         array (
          74 => 
          array (
          'title' => 'articles', 
          'path' => 'features/web-development/articles/', 
          'type' => '374', 
          'pid' => 60, 
          'hasChildren' => 0, 
         ), 
          73 => 
          array (
          'title' => 'overview', 
          'path' => 'features/web-development', 
          'type' => '118', 
          'pid' => 60, 
          'hasChildren' => 0, 
         ), 
          75 => 
          array (
          'title' => 'tutorials', 
          'path' => 'features/web-development/tutorials', 
          'type' => '374', 
          'pid' => 60, 
          'hasChildren' => 0, 
         ), 
         ), 
        ), 
        ), 
       ), 
        55 => 
        array (
        'title' => 'Activity', 
        'path' => 'activity', 
        'type' => '374', 
        'pid' => 52, 
        'hasChildren' => 0, 
       ), 
        56 => 
        array (
        'title' => 'Blog', 
        'path' => 'blog', 
        'type' => '374', 
        'pid' => 52, 
        'hasChildren' => 0, 
       ), 
        57 => 
        array (
        'title' => 'About', 
        'path' => 'about', 
        'type' => '374', 
        'pid' => 52, 
        'hasChildren' => 1, 
        'children' => 
        array (
         76 => 
         array (
         'title' => 'the author', 
         'path' => 'about/the-author', 
         'type' => '118', 
         'pid' => 57, 
         'hasChildren' => 0, 
        ), 
         77 => 
         array (
         'title' => 'the website', 
         'path' => 'about/the-website', 
         'type' => '118', 
         'pid' => 57, 
         'hasChildren' => 0, 
        ), 
        ), 
       ), 
       ), 
      ), 
      ) 

期望的結果陣列
公告第[count]指數


  Array 
      (
       [53] => Array 
        (
         [title] => Home 
         [path] => 
         [type] => 118 
         [pid] => 52 
         [mid] => 53 
         [hasChildren] => 0 
         [depth] => 1 
         [count] => 1 
        ) 

       [54] => Array 
        (
         [title] => Features 
         [path] => features 
         [type] => 374 
         [pid] => 52 
         [mid] => 54 
         [hasChildren] => 1 
         [depth] => 1 
         [count] => 2 
         [children] => Array 
          (
           [59] => Array 
            (
             [title] => artistic 
             [path] => features/artistic 
             [type] => 374 
             [pid] => 54 
             [mid] => 59 
             [hasChildren] => 1 
             [depth] => 2 
             [count] => 1 
             [children] => Array 
              (
               [63] => Array 
                (
                 [title] => galleries 
                 [path] => features/artistic/galleries 
                 [type] => 374 
                 [pid] => 59 
                 [mid] => 63 
                 [hasChildren] => 1 
                 [depth] => 3 
                 [count] => 1 
                 [children] => Array 
                  (
                   [65] => Array 
                    (
                     [title] => graphics 
                     [path] => features/artistic/galleries/graphics 
                     [type] => 118 
                     [pid] => 63 
                     [mid] => 65 
                     [hasChildren] => 0 
                     [depth] => 4 
                     [count] => 1 
                    ) 

                   [67] => Array 
                    (
                     [title] => mixed medium 
                     [path] => features/artistic/galleries/mixed-medium 
                     [type] => 118 
                     [pid] => 63 
                     [mid] => 67 
                     [hasChildren] => 0 
                     [depth] => 4 
                     [count] => 2 
                    ) 

                   [64] => Array 
                    (
                     [title] => overview 
                     [path] => features/artistic/galleries 
                     [type] => 118 
                     [pid] => 63 
                     [mid] => 64 
                     [hasChildren] => 0 
                     [depth] => 4 
                     [count] => 3 
                    ) 

                   [68] => Array 
                    (
                     [title] => photography 
                     [path] => features/artistic/galleries/photography 
                     [type] => 118 
                     [pid] => 63 
                     [mid] => 68 
                     [hasChildren] => 0 
                     [depth] => 4 
                     [count] => 4 
                    ) 

                   [66] => Array 
                    (
                     [title] => traditional 
                     [path] => features/artistic/galleries/traditional 
                     [type] => 118 
                     [pid] => 63 
                     [mid] => 66 
                     [hasChildren] => 0 
                     [depth] => 4 
                     [count] => 5 
                    ) 

                  ) 

                ) 

               [62] => Array 
                (
                 [title] => overview 
                 [path] => features/artistic 
                 [type] => 118 
                 [pid] => 59 
                 [mid] => 62 
                 [hasChildren] => 0 
                 [depth] => 3 
                 [count] => 2 
                ) 

               [69] => Array 
                (
                 [title] => tutorials 
                 [path] => features/artistic/tutorials 
                 [type] => 374 
                 [pid] => 59 
                 [mid] => 69 
                 [hasChildren] => 1 
                 [depth] => 3 
                 [count] => 3 
                 [children] => Array 
                  (
                   [71] => Array 
                    (
                     [title] => by category 
                     [path] => features/artistic/tutorials/by-category/ 
                     [type] => 118 
                     [pid] => 69 
                     [mid] => 71 
                     [hasChildren] => 0 
                     [depth] => 4 
                     [count] => 1 
                    ) 

                   [72] => Array 
                    (
                     [title] => by date 
                     [path] => features/artistic/tutorials/by-date/ 
                     [type] => 118 
                     [pid] => 69 
                     [mid] => 72 
                     [hasChildren] => 0 
                     [depth] => 4 
                     [count] => 2 
                    ) 

                   [70] => Array 
                    (
                     [title] => overview 
                     [path] => features/artistic/tutorials 
                     [type] => 118 
                     [pid] => 69 
                     [mid] => 70 
                     [hasChildren] => 0 
                     [depth] => 4 
                     [count] => 3 
                    ) 

                  ) 

                ) 

              ) 

            ) 

           [58] => Array 
            (
             [title] => overview 
             [path] => features 
             [type] => 118 
             [pid] => 54 
             [mid] => 58 
             [hasChildren] => 0 
             [depth] => 2 
             [count] => 2 
            ) 

           [61] => Array 
            (
             [title] => projects/labs 
             [path] => features/projects-labs/ 
             [type] => 374 
             [pid] => 54 
             [mid] => 61 
             [hasChildren] => 0 
             [depth] => 2 
             [count] => 3 
            ) 

           [60] => Array 
            (
             [title] => web development 
             [path] => features/web-development 
             [type] => 374 
             [pid] => 54 
             [mid] => 60 
             [hasChildren] => 1 
             [depth] => 2 
             [count] => 4 
             [children] => Array 
              (
               [74] => Array 
                (
                 [title] => articles 
                 [path] => features/web-development/articles/ 
                 [type] => 374 
                 [pid] => 60 
                 [mid] => 74 
                 [hasChildren] => 0 
                 [depth] => 3 
                 [count] => 1 
                ) 

               [73] => Array 
                (
                 [title] => overview 
                 [path] => features/web-development 
                 [type] => 118 
                 [pid] => 60 
                 [mid] => 73 
                 [hasChildren] => 0 
                 [depth] => 3 
                 [count] => 2 
                ) 

               [75] => Array 
                (
                 [title] => tutorials 
                 [path] => features/web-development/tutorials 
                 [type] => 374 
                 [pid] => 60 
                 [mid] => 75 
                 [hasChildren] => 0 
                 [depth] => 3 
                 [count] => 3 
                ) 

回答

3

我認爲這應該w^ork ...我無法測試您的示例數組,但它似乎可以在我製作的更小的數組上工作。

編輯:現在,您已經從示例數組中刪除了「深度」鍵的功能已更改。現在它發現了它自己的深度。我還添加了我的測試代碼和輸出:

<?php 

function array_depth_count(&$array, $count=array(), $depth=1) { 
    foreach ($array as &$value) { 
     if (is_array($value)) { 
      $value['count'] = ++$count[$depth]; 
      array_depth_count($value, $count, $depth + 1); 
     } 
    } 
} 

$a = array(array(array(array(0),array(0),array(),array()),0,array())); 

echo "Before\n"; 
print_r($a); 
array_depth_count($a); 
echo "\n\nAfter\n"; 
print_r($a); 

?> 

輸出:

Before 
Array 
(
    [0] => Array 
     (
      [0] => Array 
       (
        [0] => Array 
         (
          [0] => 0 
         ) 

        [1] => Array 
         (
          [0] => 0 
         ) 

        [2] => Array 
         (
         ) 

        [3] => Array 
         (
         ) 

       ) 

      [1] => 0 
      [2] => Array 
       (
       ) 

     ) 

) 

After 
Array 
(
    [0] => Array 
     (
      [0] => Array 
       (
        [0] => Array 
         (
          [0] => 0 
          [count] => 1 
         ) 

        [1] => Array 
         (
          [0] => 0 
          [count] => 2 
         ) 

        [2] => Array 
         (
          [count] => 3 
         ) 

        [3] => Array 
         (
          [count] => 4 
         ) 

        [count] => 1 
       ) 

      [1] => 0 
      [2] => Array 
       (
        [count] => 2 
       ) 

      [count] => 1 
     ) 

) 
+0

似乎無法得到這個工作的所有示例數組,我現在var_exported示例數組(在問題上編輯)。 – youdontmeanmuch 2009-02-07 10:34:33

+0

我的功能是使用原始數組中的「深度」鍵,但現在它們消失了。我編輯函數來自己查找深度,現在它似乎可以在你的示例數組上工作。 – 2009-02-07 21:15:48

0

我真的很想說這將工作

function deep(&$layer) 
{ 
    $count = 1; 
    $keys = array_keys($layer); 
    foreach($keys as $key) 
     if(is_array($layer[$key])) 
      deep($layer[$key]); 
    $layer['depth'] = $count++; 
} 

(測試對我來說工作正常) (我誤解了這個問題的另一個例子,這應該是你想要的)