douyan8961
2013-07-17 21:00
浏览 408

使用PHP解析嵌套的JSON

{
    "listing": {
        "@attributes": {
            "domain": "example.com"
        },
        "tld": "com",
        "sld": "example",
        "owner": "John Smith"
    }
}

I am needing to iterate through this JSON array and put the values into PHP variables so that I can return the values.

Example:

echo $sld;

would print:

example

Would I need to do this with a foreach loop (and if so how would I format this) or is there a easy built in function such as extract() that will do this?

图片转代码服务由CSDN问答提供 功能建议

  {
“listing”:{
“@attributes”:{
“domain”  :“example.com”
},
“tld”:“com”,
“sld”:“example”,
“”owner“:”John Smith“
} 
} 
   
 
 

我需要迭代这个JSON数组并将值放入PHP变量中,以便我可以返回值。

示例:

  echo $ sld; 
   
 
 

将打印:

< code> example

我是否需要使用foreach循环(如果是这样,我将如何格式化)或者是否有一个简单的内置函数,如 extract()会这样做吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongsha1544 2013-07-17 21:06
    已采纳
    <?php 
    $json = '{
        "listing": {
            "@attributes": {
                "domain": "example.com"
            },
            "tld": "com",
            "sld": "example",
            "owner": "John Smith"
        }
    }';
    
    $decoded_array = json_decode($json, true);
    echo $decoded_array['listing']['sld'];//example
    ?> 
    
    打赏 评论
  • duanbamo0127 2013-07-17 21:04

    You can use

    json_decode('your json string', true);
    

    Which will return an associative array of your string which you can then loop though.

    打赏 评论

相关推荐 更多相似问题