dqbr37828 2011-08-15 15:01
浏览 67
已采纳

PHP解析json_decode数组

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条回答 默认 最新

  • dtvhqlc57127 2011-08-15 23:42
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号