且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PHP将JSON转换为HTML表

更新时间:2023-02-23 11:34:13

这是您想要的代码:

$json = '{"users":[{ "user": "Carlos", "age": 30, "country": "Spain" }, { "user": "John", "age": 25, "country": "United States" }, { "user": "Mike", "age": 28, "country": "Canada" }]}';
$arr = json_decode($json, true);

echo "<table><tr><th>user</th><th>age</th><th>country</th></tr>";

foreach($arr['users'] as $item){    
    echo "<tr><td>".$item['user']."</td><td>".$item['age']."</td><td>".$item['country']."</td></tr>";
}

echo "</table>";

只需将json转换为数组,然后使用foreach并访问每个item属性.

Just convert the json to array and then use a foreach and access each item property.