Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 />';  
}
share|improve this question
1  
I don't understand what your question is? –  Pekka 웃 Aug 15 '11 at 15:02
 
THe "main code" works, BUT is there not a better/cleaner way of writing it? –  Russell Parrott Aug 15 '11 at 15:03
 
JSON is not XML. It's simply a method of representing a data structure using Javascript's code syntax. If you want a JSON string to decode into HTML, you'll have to massage the data yourself. –  Marc B Aug 15 '11 at 15:04
1  
@Russel: use a recursive function. Your code is limited to a nesting depth of 6 layers –  Marc B Aug 15 '11 at 15:07
1  
Why are you using html line breaks in an XML document? –  KTastrophy Aug 15 '11 at 15:22
show 4 more comments

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
 
many thanks, no issues just had to quickly "strtolower(str_replace(' ','',$key))" the keys just in case element names are mis-formed (will not be me doing it Thanks and voted up ty.) –  Russell Parrott Aug 16 '11 at 5:30
 
Matt - hit a slight problem / question - the very first numeric key in the foreach loop ie in very top code - [1] => stdClass Object (... I do not want to be tagged as element as it does in your function. I cannot seem to isolate it any suggestions? The reason is I want to add an attribute to the very first 'element' but not to child 'elements' –  Russell Parrott Aug 16 '11 at 8:38
 
You could do this: echo recurseJson2XML($json_array[1], true); –  Yottatron Aug 16 '11 at 17:14
add comment

There you go:

function jsonStringDecode($str, $assoc = FALSE){
    $str = substr($str, 4, strlen($str));
    $str = preg_replace("/([a-zA-Z0-9_]+?):/" , "\"$1\":", $str);
    $str=preg_replace('/.+?({.+}).+/','$1',$str); 
    $str=str_replace(';',',',$str);
    $str=str_replace(',}','}',$str);
    $str=str_replace('"s":','',$str);
    $str=str_replace('"i":','',$str);
    $str = preg_replace('/,[0-9.],/', ',', $str);
    $str = str_replace(array("\n","\r"),"",$str);
    $str = preg_replace('/([{,])(\s*)([^"]+?)\s*:/','$1"$3":',$str);

    return json_decode($str,$assoc);
}
share|improve this answer
1  
Um ... ? What is this? –  Lightness Races in Orbit Aug 15 '11 at 15:17
 
It decodes json even better than the orginal function. –  Lucas Aug 15 '11 at 15:22
 
Does the output conform to the OP's requirements? Does it work usefully on nested data? –  Lightness Races in Orbit Aug 15 '11 at 15:23
 
I don't really remember, because it was written long time ago, but I've stored this function so it means that was good. :p It does not conform to the OP's reqs probably, but it's easier to parse the output. –  Lucas Aug 15 '11 at 15:25
2  
Claim: It decodes json even better than the orginal function.. Reason: I don't really remember, because it was written long time ago, but I've stored this function so it means that was good. Nice! –  Jason McCreary Aug 15 '11 at 15:43
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.