如何将嵌套的json数据获取到php表中?json中有两个不同的表



这是我的php文件,我想从中获取json文件数据,并以php中的表格形式显示它。从json文件中获取费用表的值时,浏览器出现错误?

我得到的错误是:注意:未定义的索引:第20行C:\examplep\htdocs\jsontophptable\table.php中的金额

<!DOCTYPE html>
<html>
<body>
<?php
$url = 'accountbook.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data,true); // decode the JSON feed
?>
<table>
<tbody>
<tr>
<th>Amount </th>
<th>Detail</th>
<th>Email</th>
</tr>
<?php foreach ($characters as $character) : ?>
<tr>
<td> <?php echo $character['amounts']."n" ?> </td>
<td> <?php echo $character['details']."n"; ?> </td>
<td> <?php echo $character['email']."n;"?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>

这是我的json文件,我想从中获得嵌套数据:我想在json中获得开支表数据,作为php中的separate表和sales表separate。如何做到这一点?

{
"Expense_Details_DataBase" : {
"24-10-2018--02:15:46 PM" : {
"amounts" : "36",
"details" : "tree",
"email" : "a@gmail.com"
}
},
"Sales_Details_DataBase" : {
"24-10-2018--03:23:08 PM" : {
"email" : "b@hotmail.com",
"sale" : 1040
},
"24-10-2018--03:23:58 PM" : {
"email" : "b@hotmail.com",
"sale" : 5010
},
"24-10-2018--12:41:30 PM" : {
"email" : "a@gmail.com",
"sale" : 120
}
}
}

这是我编辑后的新php文件:编辑后的表没有显示任何数据,它是空的。如何解决。而且我想把日期和时间也放在表格里怎么做?

<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
border: 1px solid black;
}
table {
border-collapse: collapse; width: 30%;
text-align: center;
}
th {
background-color: #4CAF86;
color: white;
height: 70px;
text-align: center;
padding: 6px;
}
td{
padding: 6px;
border-bottom: 1px solid #ddd;
}
tr:hover {background-color: #f5f5f5;}
</style>
</head>
<body>
<?php
$url = 'accountbook.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data,true); // decode the JSON feed
?>
<table>
<table align="center">
<tbody>
<tr>
<th>Amount </th>
<th>Detail</th>
<th>Email</th>
</tr>
<?php
if(isset($characters['Expense_Details_DataBase'])){
foreach ($characters['Expense_Details_DataBase'] as $character) : 
?>
<tr>
<td> <?php echo $character['amounts']."n" ?> </td>
<td> <?php echo $character['details']."n"; ?> </td>
<td> <?php echo $character['email']."n;"?></td>
<?php print_r($character); ?>
</tr>
<?php endforeach;
}  ?>
</tbody>
</table>
</body>
</html> 

试试这个。

<!DOCTYPE html>
<html>
<body>
<?php
$url = 'accountbook.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data,true); // decode the JSON feed
?>
<table>
<tbody>
<tr>
<th>Amount </th>
<th>Detail</th>
<th>Email</th>
</tr>
<?php
if(isset($characters['Expense_Details_DataBase'])){
foreach ($characters['Expense_Details_DataBase'] as $character) : 
?>
<tr>
<td> <?php echo $character['amounts']."n" ?> </td>
<td> <?php echo $character['details']."n"; ?> </td>
<td> <?php echo $character['email']."n;"?></td>
</tr>
<?php endforeach;
}  ?>
</tbody>
</table>
</body>
</html>

最新更新