Précédent   Forum des professionnels en informatique > PHP > Langage > Débuter
Débuter Forum d'entraide pour débuter en PHP. Avant de poster -> Cours PHP, FAQ PHP, Outils PHP, etc.
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 28/11/2011, 15h55   #1
Membre éclairé
 
Homme Gérard Okono
Développeur Web
Inscription : juillet 2006
Messages : 711
Détails du profil
Informations personnelles :
Nom : Homme Gérard Okono
Localisation : Cameroun

Informations professionnelles :
Activité : Développeur Web
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : juillet 2006
Messages : 711
Points : 328
Points : 328
Par défaut Parcourir un array

Bonjour,
J'ai un array à plusieurs dimensions.
  • Je souhaite le parcourir pour insérer items dans une bdd.
  • Est-il possible de compter le nombre total d'items d'un tableau y compris des profondeurs.
code :
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
 
$array = Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [structure_id] => 35
                    [structure_libelle] => Présidence de la République du Cameroun
                    [structure_level] => -1
                    [structure_login] => 1-1-35-1
                    [structure_pswd] => 
                    [sub] => 
                )
 
            [1] => Array
                (
                    [structure_id] => 36
                    [structure_libelle] => Secrétariat Général
                    [structure_level] => -1
                    [structure_login] => 1-1-36-1
                    [structure_pswd] => 
                    [sub] => 
                )
 
        )
    [2] => Array
        (
            [0] => Array
                (
                    [structure_id] => 1
                    [structure_libelle] => Délégation Régionale -  Adamaoua
                    [structure_level] => 1
                    [structure_login] => 16-2-1-1
                    [structure_pswd] => 
                    [sub] => Array
                        (
                            [0] => Array
                                (
                                    [structure_id] => 1
                                    [structure_libelle] => Délégation Départementale - Djérem
                                    [structure_level] => 2
                                    [structure_login] => 16-2-1-2
                                    [structure_pswd] => 
                                    [sub] => 
                                )
 
                            [1] => Array
                                (
                                    [structure_id] => 2
                                    [structure_libelle] => Délégation Départementale - Faro et Déo
                                    [structure_level] => 2
                                    [structure_login] => 16-2-2-2
                                    [structure_pswd] => 
                                    [sub] => 
                                )
 
                        )
 
                )
 
            [1] => Array
                (
                    [structure_id] => 2
                    [structure_libelle] => Délégation Régionale -  Centre
                    [structure_level] => 1
                    [structure_login] => 16-2-2-1
                    [structure_pswd] => 
                    [sub] => Array
                        (
                            [0] => Array
                                (
                                    [structure_id] => 6
                                    [structure_libelle] => Délégation Départementale - Mfoundi
                                    [structure_level] => 2
                                    [structure_login] => 16-2-6-2
                                    [structure_pswd] => 
                                    [sub] => 
                                )
 
                            [1] => Array
                                (
                                    [structure_id] => 7
                                    [structure_libelle] => Délégation Départementale - Nyong et Kéllé
                                    [structure_level] => 2
                                    [structure_login] => 16-2-7-2
                                    [structure_pswd] => 
                                    [sub] => 
                                )
 
                            [2] => Array
                                (
                                    [structure_id] => 8
                                    [structure_libelle] => Délégation Départementale - Nyong et Mfoumou
                                    [structure_level] => 2
                                    [structure_login] => 16-2-8-2
                                    [structure_pswd] => 
                                    [sub] => 
                                )
 
                        )
 
                )
 
        )
    [3] => Array
        (
            [0] => Array
                (
                    [structure_id] => 464
                    [structure_libelle] => Grande Chancellerie des Ordres Nationaux
                    [structure_level] => -1
                    [structure_login] => 1-3-464-1
                    [structure_pswd] => 
                    [sub] => 
                )
 
        )
 
    [4] => Array
        (
            [0] => Array
                (
                    [structure_id] => 1
                    [structure_libelle] => Afghanistan
                    [structure_level] => 1
                    [structure_login] => 1-4-1-1
                    [structure_pswd] => 
                    [sub] => 
                )
 
            [1] => Array
                (
                    [structure_id] => 175
                    [structure_libelle] => Afrique du sud
                    [structure_level] => 1
                    [structure_login] => 1-4-175-1
                    [structure_pswd] => 
                    [sub] => 
                )
 
        )
 
);
Merci d'avance...
okoweb est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 15h59   #2
Modérateur
 
Inscription : septembre 2010
Messages : 7 219
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 219
Points : 8 598
Points : 8 598
utilise RecursiveArrayIterator
__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 28/11/2011, 16h22   #3
Membre éclairé
 
Homme Gérard Okono
Développeur Web
Inscription : juillet 2006
Messages : 711
Détails du profil
Informations personnelles :
Nom : Homme Gérard Okono
Localisation : Cameroun

Informations professionnelles :
Activité : Développeur Web
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : juillet 2006
Messages : 711
Points : 328
Points : 328
Citation:
Envoyé par stealth35 Voir le message
Est-ce je peux savoir pourquoi passer par cette classe que le for ou foreach habituel ?

Merci d'avance...
okoweb est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 16h29   #4
Modérateur
 
Inscription : septembre 2010
Messages : 7 219
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 219
Points : 8 598
Points : 8 598
Citation:
Envoyé par okoweb Voir le message
Est-ce je peux savoir pourquoi passer par cette classe que le for ou foreach habituel ?

Merci d'avance...
for et foreach ne font pas de récursif sur un array natif
__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 16h29   #5
Modérateur
 
Avatar de Benjamin Delespierre
 
Benjamin Delespierre
Développeur Web
Inscription : février 2010
Messages : 2 991
Détails du profil
Informations personnelles :
Nom : Benjamin Delespierre
Âge : 24
Localisation : France

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Opérateur de télécommunications

Informations forums :
Inscription : février 2010
Messages : 2 991
Points : 5 031
Points : 5 031
Parce qu'elle itère les tableaux en profondeur.
__________________
A la recherche d'un framework MVC facile a prendre en main ? Essayez Axiom
Nouveau: la référence d'Axiom est disponible sur GitHub (je la peaufine en ce moment même).

Un problème correctement identifié est à moitié résolu, évitez de poster l'intégralité de votre code avec pour seule explication "ça ne marche pas...".
Pour identifier correctement vos problèmes PHP, utilisez la gestion des erreurs et xdebug.

Les boutons et existent, servez-vous en
Benjamin Delespierre est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 17h03   #6
Membre éclairé
 
Homme Gérard Okono
Développeur Web
Inscription : juillet 2006
Messages : 711
Détails du profil
Informations personnelles :
Nom : Homme Gérard Okono
Localisation : Cameroun

Informations professionnelles :
Activité : Développeur Web
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : juillet 2006
Messages : 711
Points : 328
Points : 328
Voici une idée de ce dont je souhaite faire. Générer l'arbre html à partir du tableau.
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
//Générer l'arbre html
function displayHtml($array) {
	$html = "";
	foreach($array as $key => $items){
		$html .= '<ul class="menu-liste">';
		foreach($items as $item) {
				$html .= '<li class="menu-entree">';
				$html .= '<span>'.utf8_decode($item['structure_libelle']).'</span>';
				(( is_array($item['sub']) && count($item['sub']) > 0 )) ? $html .= displayHtml($item['sub']) : '';
				$html .= '</li>';
		}
		$html .= '</ul>';
	}
	return $html;
}
Les objets, pas encore essayé, mais je pense c'est un bon moyen pour m'y mettre.

Merci d'avance pour votre aide.
okoweb est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 17h06   #7
Expert Confirmé
 
Avatar de rawsrc
 
Homme Martin
Dev indep
Inscription : mars 2004
Messages : 1 462
Détails du profil
Informations personnelles :
Nom : Homme Martin
Âge : 35
Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

Informations professionnelles :
Activité : Dev indep

Informations forums :
Inscription : mars 2004
Messages : 1 462
Points : 2 552
Points : 2 552
Envoyer un message via Skype™ à rawsrc
Citation:
Envoyé par okoweb Voir le message
Est-il possible de compter le nombre total d'items d'un tableau y compris des profondeurs.
Bonjour,
Regarde du côté de count avec cette syntaxe :
Code :
$nb = count($array, COUNT_RECURSIVE);
__________________
# Dans la Création, tout est permis mais tout n'est pas utile...
rawsrc est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 17h11   #8
Modérateur
 
Avatar de Benjamin Delespierre
 
Benjamin Delespierre
Développeur Web
Inscription : février 2010
Messages : 2 991
Détails du profil
Informations personnelles :
Nom : Benjamin Delespierre
Âge : 24
Localisation : France

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Opérateur de télécommunications

Informations forums :
Inscription : février 2010
Messages : 2 991
Points : 5 031
Points : 5 031
Voilà ce que peuvent faire les itérateurs récursifs:
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
$arr = array(
	'a' => array(
		'a1' => array(
			'a11' => 1,
			'a12' => 2,
			'a13' => 3
		),
		'a2' => array(
			'a21' => 4,
			'a22' => 5,
			'a23' => array(
				'a231' => 'hello',
			)
		)
	),
	'b' => 6,
);
 
$arr_iterator = new RecursiveArrayIterator($arr);
$tree_iterator = new RecursiveTreeITerator($arr_iterator);
 
echo "<pre>";
foreach ($tree_iterator as $item) 
	echo $item . "\n";
echo "</pre>";
Produit:
Code :
1
2
3
4
5
6
7
8
9
10
11
|-Array
| |-Array
| | |-1
| | |-2
| | \-3
| \-Array
|   |-4
|   |-5
|   \-Array
|     \-hello
\-6
__________________
A la recherche d'un framework MVC facile a prendre en main ? Essayez Axiom
Nouveau: la référence d'Axiom est disponible sur GitHub (je la peaufine en ce moment même).

Un problème correctement identifié est à moitié résolu, évitez de poster l'intégralité de votre code avec pour seule explication "ça ne marche pas...".
Pour identifier correctement vos problèmes PHP, utilisez la gestion des erreurs et xdebug.

Les boutons et existent, servez-vous en
Benjamin Delespierre est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 17h19   #9
Membre éclairé
 
Homme Gérard Okono
Développeur Web
Inscription : juillet 2006
Messages : 711
Détails du profil
Informations personnelles :
Nom : Homme Gérard Okono
Localisation : Cameroun

Informations professionnelles :
Activité : Développeur Web
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : juillet 2006
Messages : 711
Points : 328
Points : 328
Citation:
Envoyé par rawsrc Voir le message
Bonjour,
Regarde du côté de count avec cette syntaxe :
Code :
$nb = count($array, COUNT_RECURSIVE);
Exact pour le count !
Merci.
okoweb est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 18h12   #10
Membre éclairé
 
Homme Gérard Okono
Développeur Web
Inscription : juillet 2006
Messages : 711
Détails du profil
Informations personnelles :
Nom : Homme Gérard Okono
Localisation : Cameroun

Informations professionnelles :
Activité : Développeur Web
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : juillet 2006
Messages : 711
Points : 328
Points : 328
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
39
 
$arr = array(
	'a' => array(
		'a1' => array(
			'a11' => 1,
			'a12' => 2,
			'a13' => 3
		),
		'a2' => array(
			'a21' => 4,
			'a22' => 5,
			'a23' => array(
				'a231' => 'hello',
			)
		)
	),
	'b' => 6,
);
 
function traverseStructure($iterator) {
   echo '<ul>';
    while ( $iterator -> valid() ) {
 
        if ( $iterator -> hasChildren() ) {
 
            traverseStructure($iterator -> getChildren());
 
        }
        else {
            echo '<li>'.$iterator -> key() . ' : ' . $iterator -> current() .'</li>';   
        }
 
        $iterator -> next();
    }
	echo '</ul>';
}
 
$iterator = new RecursiveArrayIterator($arr);
iterator_apply($iterator, 'traverseStructure', array($iterator));
J'essaie ce code, je n'arrive pas à obtenir l'arbre.

Merci d'avance...
okoweb est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 18h20   #11
Modérateur
 
Inscription : septembre 2010
Messages : 7 219
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 219
Points : 8 598
Points : 8 598
quelle est l’erreur ?
__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 18h28   #12
Modérateur
 
Avatar de Benjamin Delespierre
 
Benjamin Delespierre
Développeur Web
Inscription : février 2010
Messages : 2 991
Détails du profil
Informations personnelles :
Nom : Benjamin Delespierre
Âge : 24
Localisation : France

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Opérateur de télécommunications

Informations forums :
Inscription : février 2010
Messages : 2 991
Points : 5 031
Points : 5 031
Apparement pas d'erreur avec son code:
Code :
1
2
3
4
5
6
7
            a11 : 1
            a12 : 2
            a13 : 3
            a21 : 4
            a22 : 5
                a231 : hello
    b : 6
Les résultat sont les même que si tu parcourais de cette façon:
Code :
1
2
3
4
5
$iterator = new RecursiveArrayIterator($arr);
$itit = new RecursiveIteratorIterator($iterator);
 
foreach ($itit as $item)
  echo $item . '<br />';
__________________
A la recherche d'un framework MVC facile a prendre en main ? Essayez Axiom
Nouveau: la référence d'Axiom est disponible sur GitHub (je la peaufine en ce moment même).

Un problème correctement identifié est à moitié résolu, évitez de poster l'intégralité de votre code avec pour seule explication "ça ne marche pas...".
Pour identifier correctement vos problèmes PHP, utilisez la gestion des erreurs et xdebug.

Les boutons et existent, servez-vous en
Benjamin Delespierre est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 18h29   #13
Membre éclairé
 
Homme Gérard Okono
Développeur Web
Inscription : juillet 2006
Messages : 711
Détails du profil
Informations personnelles :
Nom : Homme Gérard Okono
Localisation : Cameroun

Informations professionnelles :
Activité : Développeur Web
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : juillet 2006
Messages : 711
Points : 328
Points : 328
Citation:
Envoyé par stealth35 Voir le message
quelle est l’erreur ?
Le html est mal formaté et tordu
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
<ul>
<ul>
<ul>
<li>a11 : 1</li>
<li>a12 : 2</li>
<li>a13 : 3</li>
</ul>
<ul>
<li>a21 : 4</li>
<li>a22 : 5</li>
<ul>
<li>a231 : hello</li>
</ul>
</ul>
</ul>
<li>b : 6</li>
</ul>
okoweb est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 18h36   #14
Modérateur
 
Inscription : septembre 2010
Messages : 7 219
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 219
Points : 8 598
Points : 8 598
en même temps tu fais récursif -> itératif -> récursif , donc aucun intérêt, utilise plutôt la profondeur pour créer ton arbre, mais de tout façon ça fonctionnera pas avec ton array de départ
__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 18h38   #15
Modérateur
 
Avatar de Benjamin Delespierre
 
Benjamin Delespierre
Développeur Web
Inscription : février 2010
Messages : 2 991
Détails du profil
Informations personnelles :
Nom : Benjamin Delespierre
Âge : 24
Localisation : France

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Opérateur de télécommunications

Informations forums :
Inscription : février 2010
Messages : 2 991
Points : 5 031
Points : 5 031
Si tu voulais faire une liste, ta fonction aurait dû être:
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
function makeList (RecursiveIterator $iterator) {
	$str = "<ul>";
	while ($iterator->valid()) {
		if ($iterator->hasChildren())
			$str .= '<li>' . makeList($iterator->getChildren()) . '</li>';
		else
			$str .= "<li><strong>{$iterator->key()}</strong> {$iterator->current()}</li>";
		$iterator->next();
	}
	return $str .= "</ul>";
}
 
$arr = array(
	'a' => array(
		'a1' => array(
			'a11' => 1,
			'a12' => 2,
			'a13' => 3
		),
		'a2' => array(
			'a21' => 4,
			'a22' => 5,
			'a23' => array(
				'a231' => 'hello',
			)
		)
	),
	'b' => 6,
);
 
$it = new RecursiveArrayIterator($arr);
echo makeList($it);
__________________
A la recherche d'un framework MVC facile a prendre en main ? Essayez Axiom
Nouveau: la référence d'Axiom est disponible sur GitHub (je la peaufine en ce moment même).

Un problème correctement identifié est à moitié résolu, évitez de poster l'intégralité de votre code avec pour seule explication "ça ne marche pas...".
Pour identifier correctement vos problèmes PHP, utilisez la gestion des erreurs et xdebug.

Les boutons et existent, servez-vous en
Benjamin Delespierre est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 18h50   #16
Modérateur
 
Inscription : septembre 2010
Messages : 7 219
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 219
Points : 8 598
Points : 8 598
Citation:
Envoyé par Benjamin Delespierre Voir le message
Si tu voulais faire une liste, ta fonction aurait dû être:
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
function makeList (RecursiveIterator $iterator) {
	$str = "<ul>";
	while ($iterator->valid()) {
		if ($iterator->hasChildren())
			$str .= '<li>' . makeList($iterator->getChildren()) . '</li>';
		else
			$str .= "<li><strong>{$iterator->key()}</strong> {$iterator->current()}</li>";
		$iterator->next();
	}
	return $str .= "</ul>";
}
 
$arr = array(
	'a' => array(
		'a1' => array(
			'a11' => 1,
			'a12' => 2,
			'a13' => 3
		),
		'a2' => array(
			'a21' => 4,
			'a22' => 5,
			'a23' => array(
				'a231' => 'hello',
			)
		)
	),
	'b' => 6,
);
 
$it = new RecursiveArrayIterator($arr);
echo makeList($it);
pareil tu fais récursif -> itératif -> récursif

faut faire
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
39
40
41
42
43
44
45
46
47
48
49
50
$arr = array(
    'a' => array(
        'a1' => array(
            'a11' => 1,
            'a12' => 2,
            'a13' => 3
        ),
        'a2' => array(
            'a21' => 4,
            'a22' => 5,
            'a23' => array(
                'a231' => 'hello',
            )
        )
    ),
    'b' => 6,
);
 
$it = new RecursiveIteratorIterator(
    new RecursiveArrayIterator($arr),
    RecursiveIteratorIterator::SELF_FIRST
);
 
$depth = 0;
 
echo '<ul>';
 
foreach($it as $category => $item) {
    if ($it->getDepth() > $depth) {
       echo '<ul>';
    }
 
    if ($it->getDepth() < $depth) {
          echo str_repeat('</ul></li>', $depth - $it->getDepth());
    }
 
    if($it->hasChildren()) {
        echo '<li>', $category;
    } else {
        echo '<li>', $item, '</li>';
    }
 
    $depth = $it->getDepth();
}
 
if ($it->getDepth() < $depth) {
    echo str_repeat('</ul></li>', $depth - $it->getDepth());
}
 
echo '</ul>';
__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 18h52   #17
Membre éclairé
 
Homme Gérard Okono
Développeur Web
Inscription : juillet 2006
Messages : 711
Détails du profil
Informations personnelles :
Nom : Homme Gérard Okono
Localisation : Cameroun

Informations professionnelles :
Activité : Développeur Web
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : juillet 2006
Messages : 711
Points : 328
Points : 328
Citation:
Envoyé par stealth35 Voir le message
en même temps tu fais récursif -> itératif -> récursif , donc aucun intérêt, utilise plutôt la profondeur pour créer ton arbre
Je broie du noir, pouviez vous me guider?

Citation:
Envoyé par stealth35 Voir le message
mais de tout façon ça fonctionnera pas avec ton array de départ
Pour quel raison ?
Code :
1
2
3
4
5
6
7
8
9
10
 
 [0] => Array
                (
                    [structure_id] => 35
                    [structure_libelle] => Présidence de la République du Cameroun
                    [structure_level] => -1
                    [structure_login] => 1-1-35-1
                    [structure_pswd] => 
                    [sub] => 
                )
L’intérêt pour ce array est de le parcourir de manière récursive et pour chaque sous array rencontré, appliquer une fonction qui enregistre les entrées de ce dernier en base. Et vous remarquerez tous les sous array ont la même structure.

Merci d'avance...
okoweb est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 18h53   #18
Modérateur
 
Inscription : septembre 2010
Messages : 7 219
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 219
Points : 8 598
Points : 8 598
tu peux faire un var_export de ton array
__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 19h28   #19
Membre éclairé
 
Homme Gérard Okono
Développeur Web
Inscription : juillet 2006
Messages : 711
Détails du profil
Informations personnelles :
Nom : Homme Gérard Okono
Localisation : Cameroun

Informations professionnelles :
Activité : Développeur Web
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : juillet 2006
Messages : 711
Points : 328
Points : 328
Citation:
Envoyé par stealth35 Voir le message
pareil tu fais récursif -> itératif -> récursif

faut faire
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
39
40
41
42
43
44
45
46
47
48
49
50
$arr = array(
    'a' => array(
        'a1' => array(
            'a11' => 1,
            'a12' => 2,
            'a13' => 3
        ),
        'a2' => array(
            'a21' => 4,
            'a22' => 5,
            'a23' => array(
                'a231' => 'hello',
            )
        )
    ),
    'b' => 6,
);
 
$it = new RecursiveIteratorIterator(
    new RecursiveArrayIterator($arr),
    RecursiveIteratorIterator::SELF_FIRST
);
 
$depth = 0;
 
echo '<ul>';
 
foreach($it as $category => $item) {
    if ($it->getDepth() > $depth) {
       echo '<ul>';
    }
 
    if ($it->getDepth() < $depth) {
          echo str_repeat('</ul></li>', $depth - $it->getDepth());
    }
 
    if($it->hasChildren()) {
        echo '<li>', $category;
    } else {
        echo '<li>', $item, '</li>';
    }
 
    $depth = $it->getDepth();
}
 
if ($it->getDepth() < $depth) {
    echo str_repeat('</ul></li>', $depth - $it->getDepth());
}
 
echo '</ul>';
Exacte, mais je n'ai rien compris du tout. Apprenez moi à pecher que de donner du poisson.

Merci d'avance...
okoweb est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2011, 19h35   #20
Modérateur
 
Inscription : septembre 2010
Messages : 7 219
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 219
Points : 8 598
Points : 8 598
Citation:
Envoyé par okoweb Voir le message
Apprenez moi à pecher que de donner du poisson.
c'est un forum d'entraide, pas de formation gratuite, donc ça veut dire qu'il faut aussi y mettre du tien

si c'est pour une utilisation perso : t'as tout ton temps
si c'est pour une utilisation pro : fait une formation
__________________
http://blog.stealth35.com/
stealth35 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 00h09.


 
 
 
 
Partenaires

Hébergement Web