Explications fonction recursive

kyript Messages postés 346 Date d'inscription jeudi 7 avril 2005 Statut Membre Dernière intervention 26 mars 2012 - 25 mars 2012 à 23:04
kyript Messages postés 346 Date d'inscription jeudi 7 avril 2005 Statut Membre Dernière intervention 26 mars 2012 - 26 mars 2012 à 20:21
Bonjour,

Je cherche un peu d'explication sur une fonction récursive...
J'ai rajouté dans le code quelques "echo" afin de comprendre comment la boucle fonctionne.

Mais une fois sortie du "foreach", je ne comprend pas comment la fonction est rappelé pour parcourir la suite...
(Je me demande aussi comment l'étoile * peux s'affiché seul.... Vraiment je bloque).

Pouvez-vous me donner un peu d'explications svp ?
Merci beaucoup


public function formatListeChapitre($parent, $niveau, $array) {
$html = "";
foreach($array as $noeud) {
echo '
';
echo 'Parent: '.$parent;

echo ' - ID-MERE : '.$noeud['ID_CHAP_MERE'];
echo ' - ID-CHAP : '.$noeud['ID_CHAP'];
if($parent == $noeud['ID_CHAP_MERE']) {
echo ' -  '.$noeud['TITRE_CHAP'].' - ';
for ($i = 0; $i < $niveau; $i++) {
$html .= "-"; 
}
$html .= " " . $noeud['TITRE_CHAP'] . "
";
$html .= $this->formatListeChapitre($noeud['ID_CHAP'], ($niveau + 1), $array);

}
echo '* ';
}
echo ' 
 FINISH 
';
//return $html;
}




// RESULT

Parent: 0 - ID-MERE : 0 - ID-CHAP : 1 - Chapitre 1 -
Parent: 1 - ID-MERE : 0 - ID-CHAP : 1*
Parent: 1 - ID-MERE : 1 - ID-CHAP : 2 - Sous chapitre 1 -
Parent: 2 - ID-MERE : 0 - ID-CHAP : 1*
Parent: 2 - ID-MERE : 1 - ID-CHAP : 2*
Parent: 2 - ID-MERE : 1 - ID-CHAP : 3*
Parent: 2 - ID-MERE : 1 - ID-CHAP : 4*
Parent: 2 - ID-MERE : 0 - ID-CHAP : 5*
Parent: 2 - ID-MERE : 5 - ID-CHAP : 6*
Parent: 2 - ID-MERE : 5 - ID-CHAP : 7*
Parent: 2 - ID-MERE : 2 - ID-CHAP : 8 - Sous-sous Chap 2 -
Parent: 8 - ID-MERE : 0 - ID-CHAP : 1*
Parent: 8 - ID-MERE : 1 - ID-CHAP : 2*
Parent: 8 - ID-MERE : 1 - ID-CHAP : 3*
Parent: 8 - ID-MERE : 1 - ID-CHAP : 4*
Parent: 8 - ID-MERE : 0 - ID-CHAP : 5*
Parent: 8 - ID-MERE : 5 - ID-CHAP : 6*
Parent: 8 - ID-MERE : 5 - ID-CHAP : 7*
Parent: 8 - ID-MERE : 2 - ID-CHAP : 8*
FINISH
*
FINISH
*
Parent: 1 - ID-MERE : 1 - ID-CHAP : 3 - Sous chapitre 2 -
Parent: 3 - ID-MERE : 0 - ID-CHAP : 1*
Parent: 3 - ID-MERE : 1 - ID-CHAP : 2*
Parent: 3 - ID-MERE : 1 - ID-CHAP : 3*
Parent: 3 - ID-MERE : 1 - ID-CHAP : 4*
Parent: 3 - ID-MERE : 0 - ID-CHAP : 5*
Parent: 3 - ID-MERE : 5 - ID-CHAP : 6*
Parent: 3 - ID-MERE : 5 - ID-CHAP : 7*
Parent: 3 - ID-MERE : 2 - ID-CHAP : 8*
FINISH
*
Parent: 1 - ID-MERE : 1 - ID-CHAP : 4 - Sous chapitre 3 -

1 réponse

kyript Messages postés 346 Date d'inscription jeudi 7 avril 2005 Statut Membre Dernière intervention 26 mars 2012
26 mars 2012 à 20:21
Pffff parfois je dort... Relire avec la tête froide permet de vite trouver ma connerie...

 
0
Rejoignez-nous