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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:08:04+00:00 2026-05-24T21:08:04+00:00

I have a json_array – $json_array which is multi-level the code below spits it

  • 0

I have a json_array – $json_array which is multi-level the code below “spits it” out correctly but is there a better way of doing it? What I will end up with is a structured XML document with the array keys as the element names – but they must be in the same format as the json array:

e.g.

[1] => stdClass Object ( 
    [content] => stdClass Object ( array ) 
    [general] => stdClass Object ( array ) 
    [costing] => stdClass Object ( array ) 
    [socialbits] => stdClass Object (array ) 
    [options] => stdClass Object ( (array) 
        ( [0] => stdClass Object ( array(array)  ) ) ) )

Where 1 is the main array key (actually the id from a database)

$json_array = json_encode($data);

foreach(json_decode($json_array) as $k=>$val) {

    foreach($val as $k1=>$v2){

        echo $k1;                
        echo '<br />';            
        foreach($v2 as $k3=>$v3){                
            echo $k3;                
            echo '<br />';                
            if(is_array($v3)){            
                foreach($v3 as $k4=>$v4){            
                    foreach($v4 as $k5=>$v5){                        
                        echo $k5;                            
                        echo '<br />';                                
                        foreach($v5 as $k6=>$v6){                                
                            echo $v6;                                
                            echo '<br />'
                        }           
                    }
                }
            }
            echo $v3;
        }
        echo '<br />';
    }
    echo '<br />';  
}
// } OP included the closing brace.

Thoughts and ideas most welcome thanks –

EDIT

I have no objection to edits of code, but for the sake of others please make sure they are accurate. This is a corrected form of the edit;

foreach(json_decode($json_array) as $k=>$val) {

foreach($val as $k1=>$v2){

    echo $k1;                
    echo '<br />';            
    foreach($v2 as $k3=>$v3){                
        echo $k3;                
        echo '<br />';                
        if(is_array($v3)){            
            foreach($v3 as $k4=>$v4){            
                foreach($v4 as $k5=>$v5){                        
                    echo $k5;                            
                    echo '<br />';                                
                        foreach($v5 as $k6=>$v6){                                
                            echo $v6;                                
                            echo '<br />';
                        }           
                }
            }
        } else {
        echo $v3;
        }
    }
    echo '<br />';
}
echo '<br />';  
}
  • 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-24T21:08:05+00:00Added an answer on May 24, 2026 at 9:08 pm

    I think this may help you achieve an xml representation of json which is what I assume you require from reading your question:

    <?php
    echo recurseJson2XML($json_array, true);
    function recurseJson2XML($json, $root = false)
    {
        $element = '';
        foreach($json as $key => $value)
        {
            if(is_numeric($key))
            {
                $key = 'element_' . $key;
            }
            $element .= "<{$key}>";
            if(is_array($value) || is_object($value))
            {
                $element .= recurseJson2XML($value);
            }
            else
            {
                $element .= $value;
            }
            $element .= "</{$key}>";
        }
        if($root)
        {
            $element = "<?xml version=\"1.0\" ?><root>{$element}</root>";
        }
        return $element;
    }
    ?>
    

    I tested to make sure it works on a json decoded string and it works… Let me know if you have any issues.

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

Sidebar

Related Questions

A simple question I'm sure, but I can't figure it out. I have some
I have a problem using JSON and arrays. Here is my code: while($row =
I have the following json array of objects in my code var groups =
I have a JSON array of data which is [ [[2, 5], [6, 10],
I have a complex json array which I want to target a specific line/string
[I have some code to create a JSON array. In this code I am
I have a json array which is holding the correct string independent of language
I tried many different methods but can't seem to work this one out. I
I have some PHP code that outputs a JSON array when $.getJSON is called:
I have a json array that has multiple missing numbers and is out of

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.