IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Convertir en JSON un Tableau contenant un autre Tableau (multidimentionnel) dans mon cas


Sujet :

Langage PHP

  1. #1
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Octobre 2013
    Messages
    452
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2013
    Messages : 452
    Points : 66
    Points
    66
    Par défaut Convertir en JSON un Tableau contenant un autre Tableau (multidimentionnel) dans mon cas
    Bonjour.

    Voici mon tableau PHP :

    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
        )
    );
    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
    // 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>";
    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
    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
        }
    }
    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).

    Alors, comment réparer mon convertisseur JSON pour qu'il considère ces sous-tableaux dans UssdNode l'instanciation qui représentaient children ???

  2. #2
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Octobre 2013
    Messages
    452
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2013
    Messages : 452
    Points : 66
    Points
    66
    Par défaut
    Pour comprendre l'origine de mon tableau $newMenuData, j'ai juste convertir le Tableau des Objets suivant en Array:

    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
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    Array(
        [root] => UssdNode Object
            (
                [name:UssdNode:private] => root
                [parent:UssdNode:private] => 0
                [title:UssdNode:private] => Welcome to ChegUSSD
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                        [0] => sendmoney
                        [1] => airtimedata
                        [2] => withdrawcash
                        [3] => paybill
                        [4] => buygoods
                        [5] => checkbalance
                        [6] => financialservices
                        [7] => myaccount
                        [8] => ppal
                        [9] => stripe
                        [10] => payoneer
                        [11] => skrill
                        [12] => netpay
                        [13] => btc
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [sendmoney] => UssdNode Object
            (
                [name:UssdNode:private] => sendmoney
                [parent:UssdNode:private] => root
                [title:UssdNode:private] => Send Money
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [airtimedata] => UssdNode Object
            (
                [name:UssdNode:private] => airtimedata
                [parent:UssdNode:private] => root
                [title:UssdNode:private] => Airtime/Data
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [withdrawcash] => UssdNode Object
            (
                [name:UssdNode:private] => withdrawcash
                [parent:UssdNode:private] => root
                [title:UssdNode:private] => Widthdraw Cash
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [paybill] => UssdNode Object
            (
                [name:UssdNode:private] => paybill
                [parent:UssdNode:private] => root
                [title:UssdNode:private] => Pay Bill
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                        [0] => umeme
                        [1] => nwsc
                        [2] => paytv
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [buygoods] => UssdNode Object
            (
                [name:UssdNode:private] => buygoods
                [parent:UssdNode:private] => root
                [title:UssdNode:private] => Buy Goods
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [checkbalance] => UssdNode Object
            (
                [name:UssdNode:private] => checkbalance
                [parent:UssdNode:private] => root
                [title:UssdNode:private] => Check Balance
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [financialservices] => UssdNode Object
            (
                [name:UssdNode:private] => financialservices
                [parent:UssdNode:private] => root
                [title:UssdNode:private] => Financial Services
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [myaccount] => UssdNode Object
            (
                [name:UssdNode:private] => myaccount
                [parent:UssdNode:private] => root
                [title:UssdNode:private] => My Account
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [ppal] => UssdNode Object
            (
                [name:UssdNode:private] => ppal
                [parent:UssdNode:private] => root
                [title:UssdNode:private] => PayPal
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [stripe] => UssdNode Object
            (
                [name:UssdNode:private] => stripe
                [parent:UssdNode:private] => root
                [title:UssdNode:private] => Stripe
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [payoneer] => UssdNode Object
            (
                [name:UssdNode:private] => payoneer
                [parent:UssdNode:private] => root
                [title:UssdNode:private] => Payoneer
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [skrill] => UssdNode Object
            (
                [name:UssdNode:private] => skrill
                [parent:UssdNode:private] => root
                [title:UssdNode:private] => Skrill
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [netpay] => UssdNode Object
            (
                [name:UssdNode:private] => netpay
                [parent:UssdNode:private] => root
                [title:UssdNode:private] => NetPayment
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [btc] => UssdNode Object
            (
                [name:UssdNode:private] => btc
                [parent:UssdNode:private] => root
                [title:UssdNode:private] => Bitcoin
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [umeme] => UssdNode Object
            (
                [name:UssdNode:private] => umeme
                [parent:UssdNode:private] => paybill
                [title:UssdNode:private] => UMEME touchpay
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [nwsc] => UssdNode Object
            (
                [name:UssdNode:private] => nwsc
                [parent:UssdNode:private] => paybill
                [title:UssdNode:private] => NWSC eWater
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [paytv] => UssdNode Object
            (
                [name:UssdNode:private] => paytv
                [parent:UssdNode:private] => paybill
                [title:UssdNode:private] => Pay TV
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                        [0] => air timedata
                        [1] => nws data
                        [2] => check balance
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [air timedata] => UssdNode Object
            (
                [name:UssdNode:private] => air timedata
                [parent:UssdNode:private] => paytv
                [title:UssdNode:private] => Widthdraw Cash
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [nws data] => UssdNode Object
            (
                [name:UssdNode:private] => nws data
                [parent:UssdNode:private] => paytv
                [title:UssdNode:private] => Buy Goods
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
        [check balance] => UssdNode Object
            (
                [name:UssdNode:private] => check balance
                [parent:UssdNode:private] => paytv
                [title:UssdNode:private] => My Account
                [address:UssdNode:private] => 075489956545
                [children:UssdNode:private] => Array
                    (
                    )
     
                [index:UssdNode:private] => 0
            )
     
    )
    Comment convertir ce Tableau des Objets dans un premier temps en un Tableau puis dans un second temps en JSON (en prenant en compte les children ou sous-Tableau) ???

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2013] Insertion d'un autre tableur Excel dans mon ma feuille
    Par Cabrinha dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 23/05/2017, 18h41
  2. Réponses: 0
    Dernier message: 22/01/2015, 16h56
  3. [SP-2010] Problème de connexion avec d'autres utilisateurs créés dans mon AD.
    Par skud07 dans le forum SharePoint
    Réponses: 8
    Dernier message: 12/01/2011, 20h36
  4. Réponses: 2
    Dernier message: 18/04/2007, 22h32
  5. [MySQL] un tableau ordonné dans mon while
    Par jive dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 14/02/2007, 11h03

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo