Bonjour a tous,
Nouveau sur le forum je vais essayer d'être plus claire possible.
je possède donc un arbre.
Je cherche a convertir mon nested tree en flat dans ce genre.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 array:3 [▼ 0 => array:9 [▼ "id" => 1 "parent_id" => null "_lft" => 1 "_rgt" => 18 "status" => "active" "created_at" => "2019-06-18 11:24:01" "updated_at" => "2019-06-20 11:23:14" "translate" => array:5 [▼ "id" => 1 "culture_id" => 1 "category_id" => 1 "label" => "Un principal" "slug" => "un-principal" ] "children" => array:2 [▼ 0 => array:9 [▼ "id" => 2 "parent_id" => 1 "_lft" => 2 "_rgt" => 13 "status" => "active" "created_at" => "2019-06-18 11:45:40" "updated_at" => "2019-06-20 11:10:57" "translate" => array:5 [▼ "id" => 3 "culture_id" => 1 "category_id" => 2 "label" => "azerty" "slug" => "azerty" ] "children" => array:4 [▼ 0 => array:9 [▶] 1 => array:9 [▶] 2 => array:9 [▶] 3 => array:9 [▼ "id" => 12 "parent_id" => 2 "_lft" => 11 "_rgt" => 12 "status" => "active" "created_at" => "2019-06-19 15:54:05" "updated_at" => "2019-06-20 11:52:58" "translate" => array:5 [▼ "id" => 23 "culture_id" => 1 "category_id" => 12 "label" => "sous azerty 3" "slug" => "sous-azerty-3" ] "children" => [] ] ] ] 1 => array:9 [▼ "id" => 3 "parent_id" => 1 "_lft" => 14 "_rgt" => 17 "status" => "active" "created_at" => "2019-06-18 14:07:35" "updated_at" => "2019-06-20 11:09:02" "translate" => array:5 [▶] "children" => array:1 [▼ 0 => array:9 [▼ "id" => 4 "parent_id" => 3 "_lft" => 15 "_rgt" => 16 "status" => "active" "created_at" => "2019-06-18 14:08:22" "updated_at" => "2019-06-18 14:08:22" "translate" => array:5 [▼ "id" => 7 "culture_id" => 1 "category_id" => 4 "label" => "sous querty" "slug" => "" ] "children" => [] ] ] ] ] ] 1 => array:9 [▼ "id" => 5 "parent_id" => null "_lft" => 19 "_rgt" => 22 "status" => "active" "created_at" => "2019-06-18 14:13:14" "updated_at" => "2019-06-18 14:13:14" "translate" => array:5 [▼ "id" => 9 "culture_id" => 1 "category_id" => 5 "label" => "Deux principale" "slug" => "" ] "children" => array:1 [▼ 0 => array:9 [▶] ] ] 2 => array:9 [▼ "id" => 7 "parent_id" => null "_lft" => 23 "_rgt" => 28 "status" => "active" "created_at" => "2019-06-18 16:51:31" "updated_at" => "2019-06-20 11:23:25" "translate" => array:5 [▶] "children" => array:2 [▼ 0 => array:9 [▶] 1 => array:9 [▶] ] ] ]
je suis bloqué en fonction du nombre de profondeur, je n'arrive pas reprendre le label du parent correctement.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 array:13 [▼ 0 => array:4 [▼ "id" => 1 "label" => "Un principal" "status" => "active" "created_at" => "2019-06-18T11:24:01+02:00" ] 1 => array:4 [▼ "id" => 2 "label" => "Un principal > azerty" "status" => "active" "created_at" => "2019-06-18T11:45:40+02:00" ] 2 => array:4 [▼ "id" => 6 "label" => "Un principal > azerty > sous azerty" "status" => "active" "created_at" => "2019-06-18T16:48:25+02:00" ] 3 => array:4 [▼ "id" => 9 "label" => "Un principal > azerty > sous azerty > Sous azerty 2" <-- deja erreur c'est une sous cat de sous azerty "status" => "active" "created_at" => "2019-06-18T16:53:21+02:00" ] 4 => array:4 [▼ "id" => 10 "label" => "Un principal > azerty > sous azerty > Sous azerty 2 > Sous sous azerty 2" "status" => "active" "created_at" => "2019-06-19T15:20:07+02:00" ] 5 => array:4 [▼ "id" => 12 "label" => "Un principal > azerty > sous azerty > Sous azerty 2 > Sous sous azerty 2 > sous azerty 3" <-- idem sous azerty 3 est une sous cat de sous azerty "status" => "active" "created_at" => "2019-06-19T15:54:05+02:00" ] 6 => array:4 [▼ "id" => 3 "label" => "Un principal > azerty > querty" <-- erreur "status" => "active" "created_at" => "2019-06-18T14:07:35+02:00" ] 7 => array:4 [▼ "id" => 4 "label" => "Un principal > azerty > querty > sous querty" "status" => "active" "created_at" => "2019-06-18T14:08:22+02:00" ] 8 => array:4 [▼ "id" => 5 "label" => "Deux principale" "status" => "active" "created_at" => "2019-06-18T14:13:14+02:00" ] 9 => array:4 [▼ "id" => 13 "label" => "Deux principale > sous deux 1" "status" => "active" "created_at" => "2019-06-19T16:00:16+02:00" ] 10 => array:4 [▼ "id" => 7 "label" => "Trois principal" "status" => "active" "created_at" => "2019-06-18T16:51:31+02:00" ] 11 => array:4 [▼ "id" => 11 "label" => "Trois principal > sous trois 1" "status" => "active" "created_at" => "2019-06-19T15:52:17+02:00" ] 12 => array:4 [▼ "id" => 14 "label" => "Trois principal > sous trois 1 > Sous trois 2" <-- erreur "status" => "active" "created_at" => "2019-06-20T11:34:43+02:00" ] ]
J'ai cette fonction avec laquelle je me bat.
Merci a tous ce qui pourrons me débloquer.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 $tree =[]; $traverse = function ($categories, $prefix = '') use (&$traverse, &$tree) { foreach ($categories as $category) { $prefix = $category->parent_id===null ? '':$prefix; $prefix = ($prefix) ? $prefix.' > '.$category->translate->label: $category->translate->label; $tree[] = collect([ 'id' => $category->id , 'label' => $prefix, 'status' => $category->status , 'created_at' => $category->created_at->format('c') ]); $traverse($category->children, $prefix); } return collect($tree); }; $categories = $traverse($categories);
Partager