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

  • Home
  • SEARCH
  • 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 8989565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:17:31+00:00 2026-06-15T22:17:31+00:00

Iam trying with the json_encoding for about two hours but iam not getting the

  • 0

Iam trying with the json_encoding for about two hours but iam not getting the output as required. Actually this is a requirement for the mobile application developer who is asking in the format which i will explain here.The code below is what i have tried:

include_once("class_connection.php");

//Getting the Parent Category

$sqlStr = mysql_query("select catname , id from `category` where `parentid`='0'");
$jsonArray = array();

while ($fetchStr = mysql_fetch_assoc($sqlStr)) {

    $jsonArray[] = array("ParentCategory" => $fetchStr["catname"]);
        $id = $fetchStr['id'];

    //Getting child categories from the above parent

    $sqlChildStr = mysql_query("SELECT catname,id,parentid FROM `category` where `parentid`='$id'");

    while ($fetchchildStr = mysql_fetch_assoc($sqlChildStr)) {

        $jsonArray[] = array("ChildCategory" => $fetchchildStr["catname"]);
    }

}

echo json_encode(array("JsonOutput" => $jsonArray)) . "<br />";

The Output is :

"JsonOutput":[{"ParentCategory":"Animals"},{"ChildCategory":"Bear"},{"ChildCategory":"Deer"},{"ChildCategory":"Dolphins"},
{"ParentCategory":"Art"},{"ChildCategory":"Hand Painting"},{"ChildCategory":"Painting"},{"ChildCategory":"3D"},{"ChildCategory":"Abstract"}]}

Here , in the above output the parent category array is empty without its child category array. I want to store all the child category array in its parent category array and finally i have to store both parent and child category into the JsonOutput array so i want the output as

"JsonOutput":[{
"ParentCategory":"Animals" : [{
    {"ChildCategory":"Bear"},{"ChildCategory":"Deer"},{"ChildCategory":"Dolphins"}
]}
"ParentCategory":"Arts" : [{
    {"ChildCategory":"Hand Painting"},{"ChildCategory":"Painting"},{"ChildCategory":"3D"}, {"ChildCategory":"Abstract"}
]}
]}
  • 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-15T22:17:32+00:00Added an answer on June 15, 2026 at 10:17 pm

    You probably need to do this (only the important bits are shown):

    $jsonArray = array();
    while ($parentCat = mysql_fetch_assoc($sqlStr)) {
        $temp = array(
            "ParentCategory" => $parentCat["catname"]
        );
        while ($childCat = mysql_fetch_assoc($sqlChildStr)) {
            $temp["ChildCategory"][] = array(
                "ChildCategory" => $childCat["catname"]
            );
        }
        $jsonArray[] = $temp;
    }
    

    I used a temporary variable for storing and manipulating the parent category. This gets added to the main array at the end of loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Iam trying to return json data from codeigniter view. but my code is not
I know this question has been asked many times, but I have not been
I am trying to get a JSON to iOS app, but keep getting NULL
I am trying to figure out why I am getting EXC_BAD_ACESS by this code.
I am trying to output JSON directly in my Razor view. The object being
I am trying to build JSON from two fields. Say, I have a list
I am trying to call objectForKey: on an nsdictionary ivar, but I get an
i am trying escape some data before it goes into my database, but i
I am trying to deserialize this Json: [ { Address: String content, CategoryId: 2147483647,
I have been trying to get this to work for quite a while now:

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.