Results 1 to 2 of 2
  1. #1

    PHP: Convert Mysql output to JSON (array help)

    Hi,

    I'm trying to creating a graph and I've got stuck with a bit of array manipulation... Thank you in advance for any help offered.

    I have output from my members table using a self join:

    child_id, child_name, parenta_id, parenta_name, parentb_id, parentb_name

    and I need to convert the rows which follow into output in the following format using PHP:

    Code:
    function init(){
        //init data
        var json = {
            "id": "347_0",
            "name": "Nine Inch Nails",
            "children": [{
                "id": "126510_1",
                "name": "Jerome Dillon",
                "data": {
                    "band": "Nine Inch Nails",
                    "relation": "member of band"
                },
                "children": [{
                    "id": "52163_2",
                    "name": "Howlin' Maggie",
                    "data": {
                        "band": "Jerome Dillon",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "324134_3",
                    "name": "nearLY",
                    "data": {
                        "band": "Jerome Dillon",
                        "relation": "member of band"
                    },
                    "children": []
                }]
            }, {
                "id": "173871_4",
                "name": "Charlie Clouser",
                "data": {
                    "band": "Nine Inch Nails",
                    "relation": "member of band"
                },
                "children": []
            }, {
                "id": "235952_5",
                "name": "James Woolley",
                "data": {
                    "band": "Nine Inch Nails",
                    "relation": "member of band"
                },
                "children": []
            }, {
                "id": "235951_6",
                "name": "Jeff Ward",
                "data": {
                    "band": "Nine Inch Nails",
                    "relation": "member of band"
                },
                "children": [{
                    "id": "2382_7",
                    "name": "Ministry",
                    "data": {
                        "band": "Jeff Ward",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "2415_8",
                    "name": "Revolting Cocks",
                    "data": {
                        "band": "Jeff Ward",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "3963_9",
                    "name": "Pigface",
                    "data": {
                        "band": "Jeff Ward",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "7848_10",
                    "name": "Lard",
                    "data": {
                        "band": "Jeff Ward",
                        "relation": "member of band"
                    },
                    "children": []
                }]
            }, {
                "id": "235950_11",
                "name": "Richard Patrick",
                "data": {
                    "band": "Nine Inch Nails",
                    "relation": "member of band"
                },
                "children": [{
                    "id": "1007_12",
                    "name": "Filter",
                    "data": {
                        "band": "Richard Patrick",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "327924_13",
                    "name": "Army of Anyone",
                    "data": {
                        "band": "Richard Patrick",
                        "relation": "member of band"
                    },
                    "children": []
                }]
            }, {
                "id": "2396_14",
                "name": "Trent Reznor",
                "data": {
                    "band": "Nine Inch Nails",
                    "relation": "member of band"
                },
                "children": [{
                    "id": "3963_15",
                    "name": "Pigface",
                    "data": {
                        "band": "Trent Reznor",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "32247_16",
                    "name": "1000 Homo DJs",
                    "data": {
                        "band": "Trent Reznor",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "83761_17",
                    "name": "Option 30",
                    "data": {
                        "band": "Trent Reznor",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "133257_18",
                    "name": "Exotic Birds",
                    "data": {
                        "band": "Trent Reznor",
                        "relation": "member of band"
                    },
                    "children": []
                }]
            }, {
                "id": "36352_19",
                "name": "Chris Vrenna",
                "data": {
                    "band": "Nine Inch Nails",
                    "relation": "member of band"
                },
                "children": [{
                    "id": "1013_20",
                    "name": "Stabbing Westward",
                    "data": {
                        "band": "Chris Vrenna",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "3963_21",
                    "name": "Pigface",
                    "data": {
                        "band": "Chris Vrenna",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "5752_22",
                    "name": "Jack Off Jill",
                    "data": {
                        "band": "Chris Vrenna",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "33602_23",
                    "name": "Die Warzau",
                    "data": {
                        "band": "Chris Vrenna",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "40485_24",
                    "name": "tweaker",
                    "data": {
                        "band": "Chris Vrenna",
                        "relation": "is person"
                    },
                    "children": []
                }, {
                    "id": "133257_25",
                    "name": "Exotic Birds",
                    "data": {
                        "band": "Chris Vrenna",
                        "relation": "member of band"
                    },
                    "children": []
                }]
            }, {
                "id": "236021_26",
                "name": "Aaron North",
                "data": {
                    "band": "Nine Inch Nails",
                    "relation": "member of band"
                },
                "children": []
            }, {
                "id": "236024_27",
                "name": "Jeordie White",
                "data": {
                    "band": "Nine Inch Nails",
                    "relation": "member of band"
                },
                "children": [{
                    "id": "909_28",
                    "name": "A Perfect Circle",
                    "data": {
                        "band": "Jeordie White",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "237377_29",
                    "name": "Twiggy Ramirez",
                    "data": {
                        "band": "Jeordie White",
                        "relation": "is person"
                    },
                    "children": []
                }]
            }, {
                "id": "235953_30",
                "name": "Robin Finck",
                "data": {
                    "band": "Nine Inch Nails",
                    "relation": "member of band"
                },
                "children": [{
                    "id": "1440_31",
                    "name": "Guns N' Roses",
                    "data": {
                        "band": "Robin Finck",
                        "relation": "member of band"
                    },
                    "children": []
                }]
            }, {
                "id": "235955_32",
                "name": "Danny Lohner",
                "data": {
                    "band": "Nine Inch Nails",
                    "relation": "member of band"
                },
                "children": [{
                    "id": "909_33",
                    "name": "A Perfect Circle",
                    "data": {
                        "band": "Danny Lohner",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "1695_34",
                    "name": "Killing Joke",
                    "data": {
                        "band": "Danny Lohner",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "1938_35",
                    "name": "Methods of Mayhem",
                    "data": {
                        "band": "Danny Lohner",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "5138_36",
                    "name": "Skrew",
                    "data": {
                        "band": "Danny Lohner",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "53549_37",
                    "name": "Angkor Wat",
                    "data": {
                        "band": "Danny Lohner",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "113510_38",
                    "name": "Puscifer",
                    "data": {
                        "band": "Danny Lohner",
                        "relation": "member of band"
                    },
                    "children": []
                }, {
                    "id": "113512_39",
                    "name": "Renhold\u00ebr",
                    "data": {
                        "band": "Danny Lohner",
                        "relation": "is person"
                    },
                    "children": []
                }]
            }],
            "data": []
        };

  2. #2
    Some of your data looks backwards, but i don't understand what it's supposed to be DOING, so i can't say for sure that it is. I don't use JSON either, but it LOOKS syntactically correct.

    BTW, i like your taste in music. Are you on TheSocialExpo.com? You seem like you'd hang out there.

Similar Threads

  1. [PHP] Need to convert string to Array
    By CLCook in forum Programming Discussion
    Replies: 1
    Last Post: 09-27-2008, 12:29 PM
  2. PHP, MySQL, apache on which array?
    By guimaraes83 in forum Hosting Security and Technology
    Replies: 1
    Last Post: 05-01-2006, 11:45 AM
  3. PHP: Output content in text file into array
    By Onture in forum Programming Discussion
    Replies: 5
    Last Post: 11-20-2004, 02:59 AM
  4. php - array - MySQL question
    By mcplamen in forum Programming Discussion
    Replies: 1
    Last Post: 10-12-2004, 03:48 AM
  5. [php] read file into array and output
    By kneuf in forum Programming Discussion
    Replies: 6
    Last Post: 12-30-2003, 11:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •