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 8181815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:39:00+00:00 2026-06-07T00:39:00+00:00

I have a nested drag and drop list which return json formattet string. I

  • 0

I have a nested drag and drop list which return json formattet string. I can run php serialize, json_decode, etc. on this string. But I stuck at saving the hierarchy in DB.

Example of returned output:

[{“id”:1,”children”:[{“id”:2,”children”:[{“id”:4},{“id”:7},{“id”:8}]},{“id”:3}]},{“id”:5},{“id”:6}]

…or…

Array
(
    [0] => Array
        (
            [id] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                        )

                                    [1] => Array
                                        (
                                            [id] => 7
                                        )

                                    [2] => Array
                                        (
                                            [id] => 8
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 3
                        )

                )

        )

    [1] => Array
        (
            [id] => 5
        )

    [2] => Array
        (
            [id] => 6
        )

)

I want to save this output to a DB-structure like this:

CREATE TABLE IF NOT EXISTS `menu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `rang` int(11) NOT NULL,
  `parent_id` int(11) NOT NULL,
  `name` varchar(256) NOT NULL,
  `description` varchar(256) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

Any ideas?

  • 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-06-07T00:39:02+00:00Added an answer on June 7, 2026 at 12:39 am

    I would do it this way:

    <?php
    $jsonString = '[{"id":1,"children":[{"id":2,"children":[{"id":4},{"id":7},{"id":8}]},{"id":3}]},{"id":5},{"id":6}]';
    $jsonArray = json_decode($jsonString, true);
    
    function parseJsonArray($jsonArray, $parentID = 0)
    {
      $return = array();
      foreach ($jsonArray as $subArray) {
         $returnSubSubArray = array();
         if (isset($subArray['children'])) {
           $returnSubSubArray = parseJsonArray($subArray['children'], $subArray['id']);
         }
         $return[] = array('id' => $subArray['id'], 'parentID' => $parentID);
         $return = array_merge($return, $returnSubSubArray);
      }
    
      return $return;
    }
    
    var_dump(parseJsonArray($jsonArray));
    

    You should get array like this:

    Array ( 
      [0] => Array (
        [id] => 1 
        [parentID] => 0 
      ) 
      [1] => Array (
        [id] => 2
        [parentID] => 1 
      ) 
      // ETC.
    ) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a nested list with drag/drop functionality. My issue is that I
great community :) I have category list which have nested child category list, which
I have nested list of the following form: my_list = [['Some1', '2', '3.6', '4.5',
I have a number of tables, which have nested tables. I using jQuery to
I'm working on a navigation control. I have nested lists which creates this: What
I need to design an interface, where user can drag drop elements into page
I have nested groupboxes, which logically represent nested data structures in my application. Let's
This is the scenario: I have nested classes and need to serialize then in
I have nested li, which is for horizontal subnav menu. I am trying to
i have a nested listview, i can bind the selected item of the basic

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.