PHP Array for tree format

Asked

Viewed 448 times

0

I’m trying to turn this array

Array
(
    [0] => Array
        (
            [enc] => Array
                (
                  [enc_obs] => Continua
                )

    [1] => Array
        (
            [per] => Array
                (
                    [0] => Array
                        (
                            [per_obs] => Continuar mesmo?
                        )

                    [1] => Array
                        (
                            [enc_obs] => Continua2
                            [per] => Array
                                (
                                    [0] => Array
                                        (
                                            [enc_obs] => Continua3
                                            [per] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [per_obs] => Finaliza1
                                                        )

                                                    [1] => Array
                                                        (
                                                            [per_obs] => broker9
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

In something like this

children: [
            {
                text: { name: "Account" },
                stackChildren: true,
                connectors: {
                    style: {
                        'stroke': '#8080FF',
                        'arrow-end': 'block-wide-long'
                    }
                },
                children: [
                    {
                        text: {name: "Continua"},
                        HTMLclass: "reception"
                    },
                ]
            },
            {
                text: { name: "Continuar mesmo?" },
                children: [ 
                   {

                    children: [
                            {
                                text: {name: "Continua3"}
                            },
                            {
                                children: [
                                    {
                                        text: {name: "Finaliza1"}
                                    }
                                ]
                            },
                            {
                                text: {name: "broker9"}
                            }
                        ]
                    }
                ]
            }
        ]

This code above with Children data will generate a graph similar to this

Dados do Grafico

I’ve tried several array methods for Tree etc. but it didn’t work. What has hindered me in programming is when I have the need to close with the ] and separate Children with },

This code I’m trying to generate is based on Treant.js

  • I think the confusion is in the understanding of the difference between [ and { (I’m going to take the basis Object Notation that is used in JSON). [ ] is to separate individual values: [ 1, 'Claudio', 88, { "a":2 }, 98] and { } is to separate pairs of key and value: { "id":1, nome:"Claudio", "doc":88, "flags":{ "a":2 }, "next":98 }

  • As for your question, it would be nice to [Edit] and put an example of the array and the "tree" (which would actually be an object) with the same data. For the record, PHP already has the function json_encode( ) to do this.

  • Ola Bacco, I’ve entered the above examples.. one is the array and the other is the structure I’m craving. This structure is not json.. is a completely different structure that generates this graph that I also mentioned.

  • 1

    The above example uses exactly the JSON notation, with the rules I commented on. Whether you’re using it in the context of JSON, the syntax of what you posted is exactly what I said: separate pairs with { and simple lists with [. Regarding the distribution of values, I repeat: better [Edit] and put the same collection of data before and after, because using completely different sets does not help much to realize fundamental details of the conversion you want.

  • I edited there so you can better understand... the difficulty is that this array is dynamic and I will have to generate this data from the mother array.

  • They continue 2 things that do not keep relation. What I meant is that the values of your example array should be the same as the "tree" output. You took an array example with data completely different from the output you gave in the example. I just suggested using a realistic example to avoid ambiguities for other readers and increase the chance of you getting what you’re looking for.

  • If you are curious, see the result of json_encode generating the tree of its second example, coming from array: http://ideone.com/Kzmq0V

  • I saw there that has all the same json structure... the same.. I think what I have to do is from my mother array create this array to generate the graph and then turn into json... but gave a good light already...

  • basically, when you have array( "name1" => "value1", "Nome2"=>"value2" )the Encode will generate { }, and when you have array( "value1", "value2" ) the Encode will generate [ ].

  • only all Keys with PER, 0 or 1 value will have to be transformed into Children to function

  • The rest will depend on the logic of your original data. The children is part of the Treant specification, your code will have to adapt the data as the result you want.

  • my biggest problem is really in the part of logic, to recursively read my array and manage to transform it into the format of Treant, I’ve thought of several ways and I couldn’t get a good logic to get out of it... I’m lost in it.. I think my question above is exactly this, as I could read my array and turn it into in Treant.. I’m not getting it...

Show 7 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.