使用PHP从JSON结果构建表



我刚刚开始学习PHP,我设法获得了我需要的JSON数据,但努力从JSON数据中构建表。我知道我在反复试验中做错了,但现在卡住了。

我到目前为止的php:

<?php
  ............
  $domains = json_decode(get_option('cc_whmcs_bridge_tlds'), true);
  if (count($domains->pricing)) {
    // Open the table
    echo "<table>";
    // Cycle through the array
    foreach ($domains->pricing as $idx => $tld) {
      // Output a row
      echo "<tr>";
      echo "<td>$tld->register[$idx]</td>";
      echo "<td>$tld->transfer->[$idx]</td>";
      echo "</tr>";
    }
    // Close the table
    echo "</table>";
  }
?>

JSON输出示例

{
    "currency": {
        "id": "1",
        "code": "USD",
        "prefix": "$",
        "suffix": " USD",
        "format": "2",
        "rate": "1.00000"
    },
    "pricing": {
        "com": {
            "categories": [
                "Popular",
                "gTLD"
            ],
            "addons": {
                "dns": true,
                "email": true,
                "idprotect": true
            },
            "group": "new",
            "register": {
                "1": "9.95",
                "2": "19.90",
                "3": "29.85"
            },
            "transfer": {
                "1": "9.95",
                "2": "15.00",
                "3": "25.00"
            },
            "renew": {
                "1": "9.95",
                "2": "15.00",
                "3": "25.00"
            }
        },
        "net": {
            "categories": [
                "Popular",
                "gTLD"
            ],
            "addons": {
                "dns": false,
                "email": false,
                "idprotect": false
            },
            "group": "sale",
            "register": {
                "1": "9.00"
            },
            "transfer": {
                "1": "11.95"
            },
            "renew": {
                "1": "11.95"
            }
        },
        "org": {
            "categories": [
                "Popular",
                "gTLD"
            ],
            "addons": {
                "dns": false,
                "email": false,
                "idprotect": false
            },
            "group": "hot",
            "register": {
                "1": "11.95"
            },
            "transfer": {
                "1": "11.95"
            },
            "renew": {
                "1": "11.95"
            }
        }
    }
}

我知道我的桌子php完全错误了,但是正如我所说的,第一次对我来说,这是我所拥有的。

我要渲染的桌子就像:

TLD    | REGISTER   |  TRANSFER  |    RENEW
---------------------------------------------
.com   | 1yr (9.95) | 1yr (9.95) |  1yr (9.95)
.co.uk | 1yr (9.95) | 1yr (9.95) |  1yr (9.95)

等。

您遇到的问题是循环中的元素不是数组,而是对象(特别是stdClass的实例)。您可以使用箭头操作员继续浏览它们:

$domains = json_decode($json);
foreach ($domains->pricing as $tld => $attrs) {
    echo "<tr>";
    echo "<td>".$tld."</td>";
    echo "<td>1yr (".$attrs->register->{1}.")</td>";
    echo "<td>1yr (".$attrs->transfer->{1}.")</td>";
    echo "<td>1yr (".$attrs->renew->{1}.")</td>";
    echo "</tr>";
}

演示

,您可以继续以同样的方式进行操作。例如,如果您需要显示不同年份的所有价格选项,则可以在循环内添加以下方式:

foreach ($attrs->register as $nYears => $pricePerYear) {
    echo $nYears." yrs: ".$pricePerYear;
}

和您的其他选项(更接近您最初的选项)是将true设置为json_decode()的第二个参数,它将为您提供array而不是stdClass实例。此代码同样完成:

$domains = json_decode($json, true);
foreach ($domains["pricing"] as $tld => $attrs) {
    echo "<tr>";
    echo "<td>".$tld."</td>";
    echo "<td>1yr (".$attrs["register"][1].")</td>";
    echo "<td>1yr (".$attrs["transfer"][1].")</td>";
    echo "<td>1yr (".$attrs["renew"][1].")</td>";
    echo "</tr>";
}

因此,您可以尝试以这种方式工作,以对您更舒适的人。

最新更新