Cześć! Walczę ze stworzeniem drzewka i się już zapętliłem sciana.gif
O co chodzi?
Ano mam coś takiego:
  1. <?
  2. $cats = new cats();
  3. $cats->GenerateArray();
  4. echo '<pre>';
  5. print_r($cats->CArray);
  6. echo '</pre>';
  7. class cats{
  8. public $cats = array();
  9. private $ids2 = array();
  10. public $CArray = array();
  11. function __construct(){
  12. $array = array();
  13. $array[1] = Array(
  14. 'id' => 1,
  15. 'name' => 'Realizacje',
  16. 'parent_id' => 0
  17. );
  18. $array[2] = Array
  19. (
  20. 'id' => 2,
  21. 'name' => 'Wroc?aw',
  22. 'parent_id' => 1
  23. );
  24.  
  25. $array[3] = Array
  26. (
  27. 'id' => 3,
  28. 'name' => 'Realizacje 2',
  29. 'parent_id' => 2
  30. );
  31.  
  32. $array[4] = Array
  33. (
  34. 'id' => 4,
  35. 'name' => 'Polanica',
  36. 'parent_id' => 0
  37. );
  38.  
  39. $array[5] = Array
  40. (
  41. 'id' => 5,
  42. 'name' => 'Podkategoria 3',
  43. 'parent_id' => 3
  44. );
  45.  
  46. $array[6] = Array
  47. (
  48. 'id' => 6,
  49. 'name' => 'PodPodkategoria 4',
  50. 'parent_id' => 5
  51. );
  52. $this->cats = $array;
  53. }
  54. function GenerateArray($parent=0, $level=0, $prefix=''){
  55.  
  56. if (!empty($this->cats)){
  57.  
  58. foreach ($this->cats as $item){
  59.  
  60. if (isSet($item['id']) AND !in_array($item['id'],$this->ids2)){
  61. if ($item['parent_id']==$parent){
  62. if($item['parent_id']==0)
  63. $this->CArray[$item['id']] = array('id'=>$item['id'],
  64. 'name'=>$item['name'],
  65. 'parent_id'=>$item['parent_id'],
  66. 'childs'=> array()
  67. );
  68. else $this->CArray[$item['parent_id']]['childs'][] = array('id'=>$item['id'],
  69. 'name'=>$item['name'],
  70. 'parent_id'=>$item['parent_id'],
  71. 'childs'=> array()
  72. );
  73.  
  74. $this->ids2[] = $item['id'];
  75.  
  76. $this->GenerateArray($item['id'],$level+1, $space);
  77. }
  78. }
  79. }
  80. }
  81. }
  82.  
  83. }
  84.  


i chcę, aby $this->CArray miał strukturę:

$this->CArray[ID] = (
['name'] => nazwa kategorii
'childs'=> array()//tutaj mają być "dzieci" kategorii
);

Czyli żeby powstało coś takiego:


Pozdrawiam!

problem jest z tym, żeby zagnieżdżał się w zmiennej $CArray