我有一些JSON
数据
我能够从中检索数据,但我希望数据显示在列中
JSON
{"ObjectId":43,"ObjectName":"MEGA MELA","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-22T07:56:03.767","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null},{"ObjectId":44,"ObjectName":"Event x11","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-23T00:33:50.7","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null}]}
这是我现在正在做的事情
$jsonData = json_decode($data, TRUE);
foreach ($jsonData as $d) {
foreach ($d as $ins) {
echo "<h3>".$ins['ObjectName']."</h3>";
echo "<h3>".$ins['ObjectDescription']."</h3>";
}
}
输出:
MEGA MELA
API的新活动
赛事x11
通过API进行的新活动
我想要这种格式
ObjectId | ObjectName | ObjectTitle | ObjectDescription | ObjectLabel | ObjectTypeId | MaxFieldsExpected | ObjectValueType | ObjectControlType | IsDeleted | CreatedDate | CreatedBy | EditedDate | EditedBy
43 | MEGA MELA | Event Created by API | NEW EVENT BY API | ..............................................................................
以上,我现在要借助数组键获取数据,现在我想像像keys
或JSON
这样从ObjectId
中提取ObjectName
,使它们成为表的标题并显示所有数据在列中
答案 0 :(得分:1)
在上述示例中,您可以使用key
打印文件名,而value
将成为数据。
在这里,我们可以首先循环字段名称以创建标题
我在这里使用桌子。
<table>
<?php foreach ($d as $ins) { ?>
//printing field heads
<tr>
<?php
foreach ($ins as $key=>$value) {
echo "<td><h3>".$key."</h3></td>";
} ?>
</tr>
//printing value row
<tr>
<?php
foreach ($ins as $key=>$value) {
echo "<td><h3>".$value."</h3></td>";
} ?>
</tr>
} ?>
</table>
答案 1 :(得分:1)
从这里http://www.aidanlister.com/2004/04/converting-arrays-to-human-readable-tables/
使用了array2table
的旧要意
<?php
function array2table($array, $recursive = false, $null = ' ')
{
// Sanity check
if (empty($array) || !is_array($array)) {
return false;
}
if (!isset($array[0]) || !is_array($array[0])) {
$array = array($array);
}
// Start the table
$table = "<table>\n";
// The header
$table .= "\t<tr>";
// Take the keys from the first row as the headings
foreach (array_keys($array[0]) as $heading) {
$table .= '<th>' . $heading . '</th>';
}
$table .= "</tr>\n";
// The body
foreach ($array as $row) {
$table .= "\t<tr>" ;
foreach ($row as $cell) {
$table .= '<td>';
// Cast objects
if (is_object($cell)) { $cell = (array) $cell; }
if ($recursive === true && is_array($cell) && !empty($cell)) {
// Recursive mode
$table .= "\n" . array2table($cell, true, true) . "\n";
} else {
$table .= (strlen($cell) > 0) ?
htmlspecialchars((string) $cell) :
$null;
}
$table .= '</td>';
}
$table .= "</tr>\n";
}
$table .= '</table>';
return $table;
}
$data ='[{"ObjectId":43,"ObjectName":"MEGA MELA","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-22T07:56:03.767","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null},{"ObjectId":44,"ObjectName":"Event x11","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-23T00:33:50.7","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null}]';
$jsonData = json_decode($data, TRUE);
echo array2table($jsonData);
?>
答案 2 :(得分:0)
第二个foreach应该类似于:foreach ($d as $key => $ins)
,然后在$key
变量处获得索引。