Précédent   Forum des professionnels en informatique > PHP > Langage > Fonctions
Fonctions Forum d'entraide sur les fonctions PHP. Avant de poster -> FAQ fonctions et Sources diverses
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 07/10/2011, 11h54   #1
Membre du Club
 
Inscription : janvier 2007
Messages : 236
Détails du profil
Informations forums :
Inscription : janvier 2007
Messages : 236
Points : 62
Points : 62
Par défaut Hiérarchisation d'objet dans un tableau

salut à tous...
voila, j'ai un petit pb ... de hyerarchisation de catégories ...

je récupère un résultat sql genre ça

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
array
  0 => 
    object(stdClass)[12]
      public 'idCategorie' => string '4' (length=1)
      public 'nomCategorie' => string 'Cat 1' (length=5)
      public 'parent' => null
  1 => 
    object(stdClass)[11]
      public 'idCategorie' => string '5' (length=1)
      public 'nomCategorie' => string 'cat 2' (length=5)
      public 'parent' => null
  2 => 
    object(stdClass)[10]
      public 'idCategorie' => string '6' (length=1)
      public 'nomCategorie' => string 'cat 1.1' (length=7)
      public 'parent' => string '4' (length=1)
  3 => 
    object(stdClass)[1]
      public 'idCategorie' => string '7' (length=1)
      public 'nomCategorie' => string 'cat 1.2' (length=7)
      public 'parent' => string '4' (length=1)
  4 => 
    object(stdClass)[14]
      public 'idCategorie' => string '8' (length=1)
      public 'nomCategorie' => string 'cat 1.1.1' (length=9)
      public 'parent' => string '6' (length=1)
donc dans chaque catégorie j'ai un id et l'id du parent si besoin...
j'aimerai passer tout ça dans une fonction php qui me retourne un tableau sous la forme

nom---- | id ---- | depth|
cat 1 ---| 4 -----| 0
cat1.1-- | 6 -----| 1
cat 1.1.1 | 8 ----| 2
cat1.2-- | 9 -----| 1
cat2 ----| 5 -----| 0

bien sur dans cet ordre puisque je dois l'afficher ainsi ...
vous avez une idée de comment faire ça ? parce que là je sèche

merci d'avance
++
CaviarNAS est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/10/2011, 12h14   #2
Membre du Club
 
Inscription : janvier 2007
Messages : 236
Détails du profil
Informations forums :
Inscription : janvier 2007
Messages : 236
Points : 62
Points : 62
Raah j'y suispresque mais j'arrive pas à calculer la profondeur ...

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
$tree = $db->aquery('select * from categorie');
 
$refs = array();
 
foreach ($tree as $t) {
    $thisref = &$refs[ $t->idCategorie ];
    $thisref['parent'] = $t->parent;
    $thisref['name'] = $t->nomCategorie;
trace ($thisref);
 
    if ($t->parent == NULL) {
        $list[ $t->idCategorie ] = &$thisref;
    } else {
        $refs[ $t->parent ]['children'][ $t->idCategorie ] = &$thisref;
    }
}
 
trace ($refs, 'REFS');
 
function tree ($refs, &$ok=array(), &$lvl=0) {
	foreach ($refs as $k=>$v) {
 
		if (!in_array($k, $ok)) {
 
			echo $v['name'].'('.$lvl.')';
			$ok[] = $k ;
			echo '<br />';
 
			if (isset($v['children'])) {
				$lvl ++;
				tree ($v['children'], $ok, $lvl );
			} 
 
		} 
 
	}
}
tree ($refs);
affiche

Cat 1(0)
cat 1.1(1)
cat 1.1.1(2)
cat 1.1.1(2)
cat 1.2(2)
cat 2(2)

sauf que la profondeur (entre parentheses) n'est pas la bonne
CaviarNAS est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/10/2011, 12h38   #3
Membre du Club
 
Inscription : janvier 2007
Messages : 236
Détails du profil
Informations forums :
Inscription : janvier 2007
Messages : 236
Points : 62
Points : 62
Trouvé !

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function tree ($refs, &$ok=array(), &$lvl=0) {
	foreach ($refs as $k=>$v) {
 
		if (!in_array($k, $ok)) {
 
			if ($v['parent'] == null) {				
				$lvl = 0;
			}
 
			echo $k. ' '.$v['name'].'('.$lvl.') - parent '.$v['parent'];
			$ok[] = $k ;
			echo '<br />';
 
			if (isset($v['children'])) {				
					$lvlsub = $lvl+1;										
				//	echo '---TREE--<br />';
					tree ($v['children'], $ok, $lvlsub);
			} 
 
		} 
 
	}
}
celle là j'espère qu'elle servira à du monde !
++
CaviarNAS est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/10/2011, 12h46   #4
Membre du Club
 
Inscription : janvier 2007
Messages : 236
Détails du profil
Informations forums :
Inscription : janvier 2007
Messages : 236
Points : 62
Points : 62
la même avec une utilisation par référence

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 
trace ($refs, 'REFS');
 
function tree ($refs,&$r, &$ok=array(), &$lvl=0) {
	foreach ($refs as $k=>$v) {
 
		if (!in_array($k, $ok)) {
 
			if ($v['parent'] == null) {				
				$lvl = 0;
			}
 
		//	echo $k. ' '.$v['name'].'('.$lvl.') - parent '.$v['parent'];
 
			$r[$k]['name'] = $v['name'];
			$r[$k]['depth'] = $lvl;
			$r[$k]['parent'] = $v['parent'];
 
			$ok[] = $k ;
		//	echo '<br />';
 
			if (isset($v['children'])) {				
					$lvlsub = $lvl+1;										
				//	echo '---TREE--<br />';
					tree ($v['children'], $r,$ok, $lvlsub);
			} 
 
		} 
 
	}
 
}
 
$r = '';
 tree ($refs,$r);
 
trace ($r,'R');
CaviarNAS est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/10/2011, 12h47   #5
Membre du Club
 
Inscription : janvier 2007
Messages : 236
Détails du profil
Informations forums :
Inscription : janvier 2007
Messages : 236
Points : 62
Points : 62
et la fonction trace dont je me sert ... en bonus
Code :
1
2
3
4
5
function trace($obj, $txt='') {
	echo '<pre><strong>----------------------- '.$txt.' -----------------------</strong><br />';
	print_r(var_dump($obj));
	echo '</pre>';	
}
CaviarNAS est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 01h14.


 
 
 
 
Partenaires

Hébergement Web