HTML / PHP中显示未知的嵌套级别数组/ json

时间:2014-03-21 13:15:43

标签: php arrays json

我在PHP / MYSql应用程序中工作。我在php中提供数据,如关注

[
{
    "id": "4",
    "rawId": "4",
    "displayName": "123 456",
    "name": {
        "familyName": "456",
        "formatted": "123 456",
        "givenName": "123"
    },
    "nickname": null,
    "phoneNumbers": null,
    "emails": null,
    "addresses": null,
    "ims": [
        {
            "type": -1,
            "value": ".adgjm",
            "id": "8",
            "pref": false
        }
    ],
    "organizations": null,
    "birthday": null,
    "note": null,
    "photos": null,
    "categories": null,
    "urls": null
},
{
    "id": "5",
    "rawId": "5",
    "displayName": "Dooney Evans",
    "name": {
        "middleName": "",
        "familyName": "Evans",
        "formatted": "Dooney  Evans",
        "givenName": "Dooney"
    },
    "nickname": null,
    "phoneNumbers": [
        {
            "type": "work",
            "value": "512-555-1234",
            "id": "11",
            "pref": false
        }
    ],
    "emails": null,
    "addresses": null,
    "ims": null,
    "organizations": null,
    "birthday": null,
    "note": null,
    "photos": null,
    "categories": null,
    "urls": null
},
{
    "id": "18",
    "rawId": "18",
    "displayName": "John Doe",
    "name": {
        "familyName": "Doe",
        "formatted": "John Doe",
        "givenName": "John"
    },
    "nickname": null,
    "phoneNumbers": null,
    "emails": null,
    "addresses": null,
    "ims": null,
    "organizations": null,
    "birthday": null,
    "note": null,
    "photos": null,
    "categories": null,
    "urls": null
},
{
    "id": "19",
    "rawId": "19",
    "displayName": "Rob Doe",
    "name": {
        "familyName": "Doe",
        "formatted": "Rob Doe",
        "givenName": "Rob"
    },
    "nickname": null,
    "phoneNumbers": null,
    "emails": null,
    "addresses": null,
    "ims": null,
    "organizations": null,
    "birthday": null,
    "note": null,
    "photos": null,
    "categories": null,
    "urls": null
}
]

目前它有关键功能可能有价值。可能是另一个数组,可能是对象,有时候数组和对象也会更加嵌套。

现在我正在做这个PHP来正确显示 还有另一种好的或正确的方式吗?

$data = json_decode($data);
if(is_array($data))
{
echo '<pre>';
for($i = 0; $i< count($data); $i++)
{
    $record = $data[$i];
    foreach($record as $key => $value)
    {
        if($value)
        if(is_object($value))
        {
            foreach($value as $key1 => $value1)
            {
                echo $key1." = ".$value1."<br />";
            }
        }
        else if (is_array($value))
        {
            for($j = 0; $j< count($value); $j++)
            {
                $innerValue = $value[$j];
                if(is_object($innerValue))
                {
                    foreach($innerValue as $key1 => $value1)
                    {
                        echo $key1." = ".$value1."<br />";
                    }
                }
                else if (is_array($innerValue))
                {

                }
                else
                {
                    echo $key." = ".$value."<br />";
                }
            }
        }
        else
        {
            echo $key." = ".$value."<br />";
        }
    }
    //print_r($record);
}
}

1 个答案:

答案 0 :(得分:2)

您可以尝试使用递归函数,而不是嵌套那么多: 将函数传递给一个数组,例如json_decode($json)的输出。

function print_json($json) {
    if (is_array($json) || is_object($json)) {
        echo "<table width=100%>";
        $type = 'Array';
        if(is_object($json)) $type = 'Object';
        echo '<tr><td colspan=2 style="background-color:#333333;">
                  <strong><font color=white>'.$type.'</font></strong>
              </td></tr>';
        foreach ($json as $k => $v) {
            echo '<tr><td valign="top" style="background-color:#F0F0F0;">';
            echo '<strong>'.$k.'</strong></td><td>';
            print_json($v);
            echo "</td></tr>";
        }
        echo "</table>";
        return;
    }
    echo $json;
}

Run in PHP Fiddle