在php中回显json_decode数据

时间:2016-12-28 06:40:04

标签: php json

我有一个json_encode格式的数据库值。

[["Size: 10x24 (+150)","Backing: Black","Lines: 1","Border: No Border","Border Color: ","Flashing: No Thanks","Outdoor: No","Vertical: No","contoured cut: No","qty: 1","Text 1: Custom","Line 1: Red","Font 1: Arial"]]

我想在表格中显示json_decode格式。

$options = json_decode($options, true);

然后我试图把它放在foreach循环中:

foreach ($options as $value)
{
    echo $value, "\n";
};

,输出只是Array

echo('<pre>'); print_r($json); echo('</pre>'); 

print_r()返回:

Array
(
    [0] => Array
        (
            [0] => Size: 10x24 (+150)
            [1] => Backing: Black
            [2] => Lines: 1
            [3] => Border: No Border
            [4] => Border Color: 
            [5] => Flashing: No Thanks
            [6] => Outdoor: No
            [7] => Vertical: No
            [8] => contoured cut: No
            [9] => qty: 1
            [10] => Text 1: Custom
            [11] => Line 1: Red
            [12] => Font 1: Arial
        )

)

但它看起来并不好。这是在php中回显json_decode数据的更好,更简单的方法吗?

1 个答案:

答案 0 :(得分:0)

这将在表格中打印数据。

$options = '[["Size: 10x24 (+150)","Backing: Black","Lines: 1","Border: No Border","Border Color: ","Flashing: No Thanks","Outdoor: No","Vertical: No","contoured cut: No","qty: 1","Text 1: Custom","Line 1: Red","Font 1: Arial"]]';
$options = json_decode($options, true);
echo "<table border='1'>";
foreach ($options[0] as $value)
{
    echo "<tr><td>";  
    echo $value;
    echo "</td></tr>";
};
echo "</table>";