Bonjour.
Voici mon tableau PHP :
Voici mon code PHP que j'utilise pour convertir mon tableau PHP en JSON :
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 $newMenuData = array( 'root' => new UssdNode( 'root', 0, 'Welcome to ChegUSSD', '075489956545', array( 'sendmoney', 'airtimedata', 'withdrawcash', 'paybill', 'buygoods', 'checkbalance', 'financialservices', 'myaccount', 'ppal', 'stripe', 'payoneer', 'skrill', 'netpay', 'btc' ), 0 ), 'sendmoney' => new UssdNode( 'sendmoney', 'root', 'Send Money', '075489956545', array(), 0 ), 'airtimedata' => new UssdNode( 'airtimedata', 'root', 'Airtime/Data', '075489956545', array(), 0 ), 'withdrawcash' => new UssdNode( 'withdrawcash', 'root', 'Widthdraw Cash', '075489956545', array(), 0 ), 'paybill' => new UssdNode( 'paybill', 'root', 'Pay Bill', '075489956545', array( 'umeme', 'nwsc', 'paytv' ), 0 ), 'buygoods' => new UssdNode( 'buygoods', 'root', 'Buy Goods', '075489956545', array(), 0 ), 'checkbalance' => new UssdNode( 'checkbalance', 'root', 'Check Balance', '075489956545', array(), 0 ), 'financialservices' => new UssdNode( 'financialservices', 'root', 'Financial Services', '075489956545', array(), 0 ), 'myaccount' => new UssdNode( 'myaccount', 'root', 'My Account', '075489956545', array(), 0 ), 'ppal' => new UssdNode( 'ppal', 'root', 'PayPal', '075489956545', array(), 0 ), 'stripe' => new UssdNode( 'stripe', 'root', 'Stripe', '075489956545', array(), 0 ), 'payoneer' => new UssdNode( 'payoneer', 'root', 'Payoneer', '075489956545', array(), 0 ), 'skrill' => new UssdNode( 'skrill', 'root', 'Skrill', '075489956545', array(), 0 ), 'netpay' => new UssdNode( 'netpay', 'root', 'NetPayment', '075489956545', array(), 0 ), 'btc' => new UssdNode( 'btc', 'root', 'Bitcoin', '075489956545', array(), 0 ), 'umeme' => new UssdNode( 'umeme', 'paybill', 'Umeme', '075489956545', array(), 0 ), 'nwsc' => new UssdNode( 'nwsc', 'paybill', 'NWSC', '075489956545', array(), 0 ), 'paytv' => new UssdNode( 'paytv', 'paybill', 'PayTV', '075489956545', array( 'gotv', 'dstv', 'startimes' ), 0 ), 'gotv' => new UssdNode( 'gotv', 'paytv', 'GOtv', '075489956545', array(), 0 ), 'dstv' => new UssdNode( 'dstv', 'paytv', 'DStv', '075489956545', array(), 0 ), 'startimes' => new UssdNode( 'startimes', 'paytv', 'StarTimes', '075489956545', array(), 0 ) );
Et voici le résultat JSON que j'obtiens :
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 // Create a new array without the instantiated objects $jsonArray = array_map(function($node) { return [ 'name' => $node->getName(), 'parent' => $node->getParent(), 'title' => $node->getTitle(), 'address' => $node->getAddress(), 'children' => $node->getChildren(), 'index' => $node->getIndex() ]; }, $newMenuData); // Convert the PHP array to JSON $jsonData = json_encode($jsonArray, JSON_PRETTY_PRINT); // Output the JSON data echo "<pre>"; print_r($jsonData); echo "</pre>";
Comme vous pouvez le voir, dans ma sortie, tout children est vide, y compris children (sous-tableau de UssdNode) de 'paybill' => new UssdNode(... array(... ), 0) et 'paytv' => new UssdNode(... array(... ), 0).
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 { "root": { "name": 0, "parent": "Welcome to ChegUSSD", "title": "root", "address": "", "children": [], "index": 0 }, "sendmoney": { "name": "root", "parent": "Send Money", "title": "sendmoney", "address": "", "children": [], "index": 0 }, "airtimedata": { "name": "root", "parent": "Airtime\/Data", "title": "airtimedata", "address": "", "children": [], "index": 0 }, "withdrawcash": { "name": "root", "parent": "Widthdraw Cash", "title": "withdrawcash", "address": "", "children": [], "index": 0 }, "paybill": { "name": "root", "parent": "Pay Bill", "title": "paybill", "address": "", "children": [], "index": 0 }, "buygoods": { "name": "root", "parent": "Buy Goods", "title": "buygoods", "address": "", "children": [], "index": 0 }, "checkbalance": { "name": "root", "parent": "Check Balance", "title": "checkbalance", "address": "", "children": [], "index": 0 }, "financialservices": { "name": "root", "parent": "Financial Services", "title": "financialservices", "address": "", "children": [], "index": 0 }, "myaccount": { "name": "root", "parent": "My Account", "title": "myaccount", "address": "", "children": [], "index": 0 }, "ppal": { "name": "root", "parent": "PayPal", "title": "ppal", "address": "", "children": [], "index": 0 }, "stripe": { "name": "root", "parent": "Stripe", "title": "stripe", "address": "", "children": [], "index": 0 }, "payoneer": { "name": "root", "parent": "Payoneer", "title": "payoneer", "address": "", "children": [], "index": 0 }, "skrill": { "name": "root", "parent": "Skrill", "title": "skrill", "address": "", "children": [], "index": 0 }, "netpay": { "name": "root", "parent": "NetPayment", "title": "netpay", "address": "", "children": [], "index": 0 }, "btc": { "name": "root", "parent": "Bitcoin", "title": "btc", "address": "", "children": [], "index": 0 }, "umeme": { "name": "paybill", "parent": "Umeme", "title": "umeme", "address": "", "children": [], "index": 0 }, "nwsc": { "name": "paybill", "parent": "NWSC", "title": "nwsc", "address": "", "children": [], "index": 0 }, "paytv": { "name": "paybill", "parent": "PayTV", "title": "paytv", "address": "", "children": [], "index": 0 }, "gotv": { "name": "paytv", "parent": "GOtv", "title": "gotv", "address": "", "children": [], "index": 0 }, "dstv": { "name": "paytv", "parent": "DStv", "title": "dstv", "address": "", "children": [], "index": 0 }, "startimes": { "name": "paytv", "parent": "StarTimes", "title": "startimes", "address": "", "children": [], "index": 0 } }
Alors, comment réparer mon convertisseur JSON pour qu'il considère ces sous-tableaux dans UssdNode l'instanciation qui représentaient children ???
Partager