2009-01-12 5 views
4

Par le tableau d'exemple tout en bas, je veux être en mesure d'ajouter la profondeur de chaque tableau incorporé à l'intérieur du tableau. par exemple:PHP Arrays, ajout de récursivité de l'élément tableau récursivement à un tableau avec la clé de 'profondeur'

 
    array (
     53 => 
     array (
     'title' => 'Home', 
     'path' => '', 
     'type' => '118', 
     'pid' => 52, 
     'hasChildren' => 0, 
    ), 

a une profondeur d'une fonction du tableau d'exemple ci-dessous, il devrait ressembler à ceci:

 
    array (
     53 => 
     array (
     'title' => 'Home', 
     'path' => '', 
     'type' => '118', 
     'pid' => 52, 
     'hasChildren' => 0, 
     'depth' => 1, 
    ), 

et ainsi de suite ...

Tous les tentatives de la fonction de tableau récursif que j'ai fait sont assez embarrassant. Cependant, j'ai regardé RecursiveArrayIterator qui a la fonction getDepth. Je suis confus sur la façon de l'ajouter au tableau actuel ... toute aide est très appréciée, merci.

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

print_r($example); 

?> 

Répondre

8

Je suppose qu'il existe un autre tableau (en haut non inclus dans votre exemple de code) Quelque chose comme ça?

function array_set_depth($array, $depth = -1) 
{ 
    $subdepth = $depth + 1; 
    if ($depth < 0) { 
    foreach ($array as $key => $subarray) { 
     $temp[$key] = array_set_depth(($subarray), $subdepth); 
    } 
    } 
    if ($array['hasChildren'] && isset($array['children'])) { 
    foreach ($array['children'] as $key => $subarray) { 
     $temp[$key] = array_set_depth($subarray, $subdepth); 
    } 
    $array['children'] = $temp; 
    } 
    $array['depth'] = $depth; 
    return $array; 
} 

Exemple d'utilisation, je mets votre tableau à la valeur $ a:

$b = array_set_depth($a); 
print_r($b); 

Edit:

Pour définir la profondeur avant que les enfants pour l'impression agréable que vous pouvez faire ceci:

function array_set_depth($array, $depth = -1) 
{ 
    $subdepth = $depth + 1; 
    if ($depth < 0) { 
    foreach ($array as $key => $subarray) { 
     $temp[$key] = array_set_depth(($subarray), $subdepth); 
    } 
    return $temp; 
    } 
    $array['depth'] = $depth; 
    if ($array['hasChildren'] && isset($array['children'])) { 
    foreach ($array['children'] as $key => $subarray) { 
     $temp[$key] = array_set_depth($subarray, $subdepth); 
    } 
    unset($array['children']); 
    $array['children'] = $temp; 
    } 
    return $array; 
} 
5

Une fonction récursive comme celle-ci devrait le faire? La chose à noter est que le tableau est passé par référence, afin que nous puissions le modifier. Notez que nous utilisons également cette référence dans l'appel récursif à setDepth. Bien que j'aie utilisé foreach pour plus de commodité, la variable $ value est une copie, et le passage à setDepth n'apporterait que des changements de courte durée dans la portée de la boucle foreach.

+0

C'est une fonction récursive générique pour les tableaux. Besoin de travail pour cet exemple. – OIS

+0

J'ai modifié votre code pour qu'il fonctionne comme il se doit. Dunno comment l'afficher. – OIS

0

STH comme celui-ci devrait faire l'affaire:

function setdepth($arr, $depth = 0) 
{ 
    foreach ($arr as $key => $val) 
    { 
     $arr[$key]['depth'] = $depth; 
     if ($arr[$key]['hasChildren']) 
     { 
      setdepth(&$arr[$key]['children'], $depth+1); 
     } 
    } 
} 

je serais plus facile si votre tableau a commencé avec l'index non avec des valeurs, de sorte que l'utilisation de l'exemple pourrait ressembler à ceci:

$arr[0] = $website; 
setdepth(&$arr, 0); 

où le site web est le tableau de votre exemple

+0

@youdontmeanmuch - avez-vous des erreurs? le résultat est dans la variable $ arr – empi

4

Code Pauls modifié pour fonctionner avec cet exemple.

function setDepth(&$a, $depth = -1) 
{ 
    if (($depth > -1) && !($depth % 2)) 
     $a['depth']= $depth/2; 
    foreach($a as $key=>$value) 
    { 
     if (is_array($value)) 
      setDepth($a[$key], $depth+1); 
    } 

} 
setDepth($a); 
print_r($a); 
0

Cela pourrait être utile:

function extend($arr, $myArr=array()) { 

    foreach($arr as $key => $value) { 
     if(is_array($key)) { 
      extend($arr[ $key ]); 
     } else { 
      $myArr[ $key ] = $arr[ $key ]; 
     } 
    } 

    return $myArr; 
} 

Fonction appelée « extension », car il est tableau des copies non seulement dans une nouvelle, il peut s'étend également des réseaux existants.

Pour étendre un tableau, vous devez le mettre comme second paramètre, sinon placez un tableau vide. La fonction supprime les propriétés du tableau et vérifie s'il s'agit d'un tableau ou non et si la fonction est à nouveau invoquée, sinon elle copie les valeurs dans un autre tableau et les renvoie.

Questions connexes