par
FuZZyLine » 27 juil. 2009, 18:39
Bonjour,
je dois modifier l'affichage d'un programme open source que jutilise, cest l'affichage des catégories.
je veux faire afficher les catégories avec en dessous les sous-catégories
dans le template de la page de catégorie tout est contenu dans la variable $categories
Si je fais var_dump($categories);
J'obtien:
array(4) { [0]=> object(stdClass)#48 (6) { ["link"]=> string(6) "list/1" ["title"]=> string(4) "Cars" ["description"]=> string(120) "A lot of cars, different classes, little and large ones, different manufacturers, you can check for the price as well..." ["itemNum"]=> string(1) "6" ["picture"]=> string(21) "./pictures/cats/1.jpg" ["subCats"]=> array(2) { [0]=> array(2) { ["link"]=> string(33) "Economy cars" ["name"]=> string(12) "Economy cars" } [1]=> array(2) { ["link"]=> string(28) "Pickups" ["name"]=> string(7) "Pickups" } } } [1]=> object(stdClass)#27 (6) { ["link"]=> string(6) "list/2" ["title"]=> string(8) "Hardware" ["description"]=> string(104) "Find hardware into your computer, from memory to CD-Rom, from hard disk to monitor, whatever you want..." ["itemNum"]=> string(1) "4" ["picture"]=> string(21) "./pictures/cats/2.jpg" ["subCats"]=> array(2) { [0]=> array(2) { ["link"]=> string(31) "Hard disks" ["name"]=> string(10) "Hard disks" } [1]=> array(2) { ["link"]=> string(33) "Memory chips" ["name"]=> string(12) "Memory chips" } } } [2]=> object(stdClass)#56 (6) { ["link"]=> string(6) "list/3" ["title"]=> string(8) "Cottages" ["description"]=> string(108) "Find a house for your family! Nice weekends, fishing in the lake, climbing to the mountains, near to your..." ["itemNum"]=> string(1) "6" ["picture"]=> string(21) "./pictures/cats/3.jpg" ["subCats"]=> array(0) { } } [3]=> object(stdClass)#49 (6) { ["link"]=> string(6) "list/4" ["title"]=> string(6) "Dating" ["description"]=> string(110) "Are you searching for your partner for a long time? The search is over, thousands are looking for love here..." ["itemNum"]=> string(1) "3" ["picture"]=> string(21) "./pictures/cats/4.jpg" ["subCats"]=> array(2) { [0]=> array(2) { ["link"]=> string(24) "Men" ["name"]=> string(3) "Men" } [1]=> array(2) { ["link"]=> string(27) "Women" ["name"]=> string(5) "Women" } } } }
comment puis construire une boucle qui maffichera celà de façon coérante ?
Merci!
Salut,
Le plus simple:
foreach($array AS $key => $value)
{
echo $key." // ".$value."<br />\n";
}
Le problème c'est que ce n'est valable que pour un tableau/objet à 1 dimension.
Dans un cas plus complex je te conseille de te tourner toi vers les fonctions SPL
(implements iterator) et/voir vers une fonction récursive.
http://julien-pauli.developpez.com/tuto ... spl/#LII-A
---
Si ca peut t'aider (hors spl) ce que tu cherches à faire c'est "reproduire" une fonction de tri.
...ou se qu"on appelait fonction de tri.
Bon code @+

[quote="stefane321"]
Bonjour,
je dois modifier l'affichage d'un programme open source que jutilise, cest l'affichage des catégories.
je veux faire afficher les catégories avec en dessous les sous-catégories
dans le template de la page de catégorie tout est contenu dans la variable $categories
Si je fais var_dump($categories);
J'obtien:
[quote]array(4) { [0]=> object(stdClass)#48 (6) { ["link"]=> string(6) "list/1" ["title"]=> string(4) "Cars" ["description"]=> string(120) "A lot of cars, different classes, little and large ones, different manufacturers, you can check for the price as well..." ["itemNum"]=> string(1) "6" ["picture"]=> string(21) "./pictures/cats/1.jpg" ["subCats"]=> array(2) { [0]=> array(2) { ["link"]=> string(33) "Economy cars" ["name"]=> string(12) "Economy cars" } [1]=> array(2) { ["link"]=> string(28) "Pickups" ["name"]=> string(7) "Pickups" } } } [1]=> object(stdClass)#27 (6) { ["link"]=> string(6) "list/2" ["title"]=> string(8) "Hardware" ["description"]=> string(104) "Find hardware into your computer, from memory to CD-Rom, from hard disk to monitor, whatever you want..." ["itemNum"]=> string(1) "4" ["picture"]=> string(21) "./pictures/cats/2.jpg" ["subCats"]=> array(2) { [0]=> array(2) { ["link"]=> string(31) "Hard disks" ["name"]=> string(10) "Hard disks" } [1]=> array(2) { ["link"]=> string(33) "Memory chips" ["name"]=> string(12) "Memory chips" } } } [2]=> object(stdClass)#56 (6) { ["link"]=> string(6) "list/3" ["title"]=> string(8) "Cottages" ["description"]=> string(108) "Find a house for your family! Nice weekends, fishing in the lake, climbing to the mountains, near to your..." ["itemNum"]=> string(1) "6" ["picture"]=> string(21) "./pictures/cats/3.jpg" ["subCats"]=> array(0) { } } [3]=> object(stdClass)#49 (6) { ["link"]=> string(6) "list/4" ["title"]=> string(6) "Dating" ["description"]=> string(110) "Are you searching for your partner for a long time? The search is over, thousands are looking for love here..." ["itemNum"]=> string(1) "3" ["picture"]=> string(21) "./pictures/cats/4.jpg" ["subCats"]=> array(2) { [0]=> array(2) { ["link"]=> string(24) "Men" ["name"]=> string(3) "Men" } [1]=> array(2) { ["link"]=> string(27) "Women" ["name"]=> string(5) "Women" } } } }[/quote]
comment puis construire une boucle qui maffichera celà de façon coérante ?
Merci!
[/quote]
Salut,
Le plus simple:
[php]
foreach($array AS $key => $value)
{
echo $key." // ".$value."<br />\n";
}
[/php]
Le problème c'est que ce n'est valable que pour un tableau/objet à 1 dimension.
Dans un cas plus complex je te conseille de te tourner toi vers les fonctions SPL
(implements iterator) et/voir vers une fonction récursive.
http://julien-pauli.developpez.com/tutoriels/php/spl/#LII-A
---
Si ca peut t'aider (hors spl) ce que tu cherches à faire c'est "reproduire" une fonction de tri.
...ou se qu"on appelait fonction de tri.
Bon code @+ ;)