Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7015083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:36:22+00:00 2026-05-27T22:36:22+00:00

I want to tranform an php array into an json string to use with

  • 0

I want to tranform an php array into an json string to use with JavaScript InfoVis Toolkit formart.

the objective:
InfoVis Demo Tree

Json specification format:
InfoVis-loading and serving JSON data

I have this php array: $my_array :

Array 
( 
    [item_1] => Array 
        ( 
            [id] => item_1_ID 
            [name] => item_1_NAME 
            [data] => item_1_DATA 
            [children] => Array 
                ( 
                    [door] => Array 
                        ( 
                            [id] => door_ID 
                            [name] => door_NAME 
                            [data] => door_DATA 
                            [children] => Array 
                            ( 
                                [mozart] => Array 
                                    ( 
                                        [id] => mozart_ID 
                                        [name] => mozart_NAME 
                                        [data] => mozart_DATA 
                                        [children] => Array 
                                            ( 
                                                [grass] => Array 
                                                    ( 
                                                        [id] => grass_ID 
                                                        [name] => grass_NAME 
                                                        [data] => yes 
                                                    ) 

                                                [green] => Array 
                                                    ( 
                                                        [id] => green_ID 
                                                        [name] => green_NAME 
                                                        [data] => no 
                                                    ) 

                                                [human] => Array 
                                                    ( 
                                                        [id] => human_ID 
                                                        [name] => human_NAME 
                                                        [data] => human_DATA 
                                                        [children] => Array 
                                                            ( 
                                                                [blue] => Array 
                                                                    ( 
                                                                        [id] => blue_ID 
                                                                        [name] => blue_NAME 
                                                                        [data] => blue_DATA 
                                                                        [children] => Array 
                                                                            ( 
                                                                                [movie] => Array 
                                                                                    ( 
                                                                                        [id] => movie_ID 
                                                                                        [name] => movie_NAME 
                                                                                        [data] => yes 
                                                                                    ) 

                                                                            ) 

                                                                    ) 

                                                            ) 

                                                    ) 

                                            ) 

                                    ) 

                            ) 

                    ) 

                [beat] => Array 
                    ( 
                        [id] => beat_ID 
                        [name] => beat_NAME 
                        [data] => yes 
                    ) 

                [music] => Array 
                    ( 
                        [id] => music_ID 
                        [name] => music_NAME 
                        [data] => no 
                    ) 

            ) 

    ) 

)           

now if I json_encode($my_array);

{ 
    "item_1": { 
        "id": "item_1_ID", 
        "name": "item_1_NAME", 
        "data": "item_1_DATA", 
        "children": { 
            "door": { 
                "id": "door_ID", 
                "name": "door_NAME", 
                "data": "door_DATA", 
                "children": { 
                    "mozart": { 
                        "id": "mozart_ID", 
                        "name": "mozart_NAME", 
                        "data": "mozart_DATA", 
                        "children": { 
                            "grass": { 
                                "id": "grass_ID", 
                                "name": "grass_NAME", 
                                "data": "yes" 
                            }, 
                            "green": { 
                                "id": "green_ID", 
                                "name": "green_NAME", 
                                "data": "no" 
                            }, 
                            "human": { 
                                "id": "human_ID", 
                                "name": "human_NAME", 
                                "data": "human_DATA", 
                                "children": { 
                                    "blue": { 
                                        "id": "blue_ID", 
                                        "name": "blue_NAME", 
                                        "data": "blue_DATA", 
                                        "children": { 
                                            "movie": { 
                                                "id": "movie_ID", 
                                                "name": "movie_NAME", 
                                                "data": "yes" 
                                            } 
                                        } 
                                    } 
                                } 
                            } 
                        } 
                    } 
                } 
            }, 
            "beat": { 
                "id": "beat_ID", 
                "name": "beat_NAME", 
                "data": "yes" 
            }, 
            "music": { 
                "id": "music_ID", 
                "name": "music_NAME", 
                "data": "no" 
            } 
        } 
    } 
}  

But to InfoVis the current json output (json_encode($my_array)) has 3 problems:

  1. is not using [ ]
  2. the ‘children’ arrays have the key names
  3. arrays items is with their key names -> example: “item_1”: { ….

let me point the problem so maybe you can help with an function to transform this json string:

see this slice of json_encode($my_array) output:

{  
    "item_1": {  
        "id": "item_1_ID",  
        "name": "item_1_NAME",  
        "data": "item_1_DATA",  
        "children": {  
            "door": {  
                "id": "door_ID",  

1. problem 1:

{  
    "item_1": {  

we have to remove those keys like: “item_1”:

2. problem 2:

"children": {  
            "door": {  
                "id": "door_ID",  

the correct code for this should be:

"children": [  
            {  
                "id": "door_ID",......  

“door”: was removed… because it`s a key

“children”: { => becomes” “children”: [

An working example of ‘children’:

"children": [  
    {  
        "id": "grass_ID",  
        "name": "grass_NAME",  
        "data": "yes"  
    },  
    {  
        "id": "green_ID",  
        "name": "green_NAME",  
        "data": "no"  
    }  
]  

to clarify an complete example of WORKING Json InfoVis format:

json = {
        id: "node02",
        name: "0.2",
        children: [{
            id: "node13",
            name: "1.3",
            children: [{
                id: "node24",
                name: "2.4"
              }, {
                id: "node222",
                name: "2.22"
              }]
        }, {
            id: "node125",
            name: "1.25",
            children: [{
                id: "node226",
                name: "2.26"
            }, {
                id: "node237",
                name: "2.37"
            }, {
                id: "node258",
                name: "2.58"
            }]
        }, {
            id: "node165",
            name: "1.65",
            children: [{
                id: "node266",
                name: "2.66"
            }, {
                id: "node283",
                name: "2.83"
            }, {
                id: "node2104",
                name: "2.104"
            }, {
                id: "node2109",
                name: "2.109"
            }, {
                id: "node2125",
                name: "2.125"
            }]
        }, {
            id: "node1130",
            name: "1.130",
            children: [{
                id: "node2131",
                name: "2.131"
            }, {
                id: "node2138",
                name: "2.138"
            }]
        }]
    };

is it clear to understand?

Hope anyone can help me.. I’m working on this for days!

thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:36:23+00:00Added an answer on May 27, 2026 at 10:36 pm

    Try this quickie conversion function

    function fixit($yourArray) {
        $myArray = array();
        foreach ($yourArray as $itemKey => $itemObj) {
            $item = array();
            foreach ($itemObj as $key => $value) {
                if (strtolower($key) == 'children') {
                    $item[$key] = fixit($value);
                } else {
                    $item[$key] = $value;
                }
            }
            $myArray[] = $item;
        }
        return $myArray;
    }
    
    $fixed = fixit($my_array);
    $json = json_encode($fixed);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to use JSONP to return an array from PHP to JavaScript.
I want to transform the 'values' array created by xml_parse_into_struct() into a bunch of
I've a php language file, that lists values in an array. I want to
I want to transform a List[Option[T]] into a Option[List[T]] . The signature type of
I have a datetime column in db that I want to transform into a
Given the following sequence: normal %(b)BOLD% normal I want to transform this into the
I have a string Word1 Word2 and I want to transform it to a
My goals: I want to have #nav to be divide into menu-left and menu-right,
i want to transform some xml into HTML that has the following format: <TR><TD>
I'm reading 8 bytes from a socket in PHP, and want to transform them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.