2009-02-07 6 views
2

Pointless DribblePHP tableaux, annexant nombre d'éléments du tableau récursive à un tableau


Ok Ceci est une autre bizarre un de moi, je veux remercier OIS pour me aider ma dernière question ... qui traite de ce même type de manipulation de tableau funky ... j'ai étudié ce code en profondeur et je pense qu'il m'a aidé à devenir meilleur avec des fonctions de manipulation de tableau récursives. Cependant, je trouve encore une fois moi-même dans un autre endroit difficile

Problème actuel


Je suis en train d'écrire une fonction manipulatrice récursive telle que this. Sauf pour obtenir la profondeur de chaque élément du tableau. Il ajoutera un compte incrémental à chaque élément de tableau avec une certaine profondeur. La façon la plus simple de voir ce que j'essaie de faire est de voir le "tableau d'échantillons" et le "tableau de résultats souhaité" ... J'ai l'impression de mieux comprendre ces fonctions récursives. mais celui-ci me donne l'enfer, merci d'avance pour toute sorte d'aide que vous pouvez me donner avec ceci. Veuillez ne pas tenir compte du résultat [profondeur] du tableau d'échantillons. J'ai déjà une fonction qui ajoute ceci. Merci encore, - YouDontMeanMuch

Array Sample


  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, 
        ), 
        ), 
       ), 
       ), 
      ), 
      ) 

Résultat souhaité tableau
Notez l'index


  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 
                ) 

Répondre

3

Je pense que [count] cela devrait w ork ... Je n'ai pas pu tester sur votre tableau d'exemple mais il semble fonctionner sur un plus petit tableau que j'ai fait.

Modifier: Modifié la fonction maintenant que vous avez supprimé les clés 'depth' de votre tableau exemple. Maintenant, il trouve la profondeur par lui-même. J'ai aussi ajouté mon code de test et de sortie:

<?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); 

?> 

Sortie:

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

Impossible de faire fonctionner cela du tout le tableau d'échantillons, i var_exported le tableau d'échantillons maintenant (éditer sur la question). – youdontmeanmuch

+0

Ma fonction utilisait les touches 'depth' dans le tableau d'origine mais maintenant elles sont parties. J'ai édité la fonction pour trouver la profondeur par elle-même et il semble fonctionner sur votre tableau d'échantillons maintenant. –

+0

Génial, ça a du sens ... et ça marche: D merci beaucoup! – youdontmeanmuch

0

Je veux vraiment dire cela fonctionnera

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

(testé et fonctionne bien pour moi) (Un autre cas de moi mal compris la question. Cela devrait être ce que vous voulez)

+0

Oui, je pense que vous avez mal compris la question (désolé je n'ai pas précisé) J'ai déjà une fonction pour définir la profondeur, j'ai besoin de quelque chose pour définir le [nombre] sur le tableau d'échantillons. – youdontmeanmuch

Questions connexes