douan8473 2015-02-18 12:35 采纳率: 100%
浏览 51
已采纳

数组的json_decode在HTML输出中无法正确呈现

I have a database with some data encoded in json format. I would like to output an HTML table but I ran into a snag when the particular piece of data in the json encoded data cell was an array. The word "Array" is output for the field in the HTML table along with "Notice: Array to string conversion in...".

My code uses a MySQL statement to retrieve data and the usual fetch array operators. The specific code used to render the json encoded data is:

$user_params = json_decode($row['params'], true);

and to create the output:

<td>{$user_params['phone']}</td>
<td>{$user_params['ride_catagory']}</td>

The first line above renders correctly since the data is encoded as a single text string, however, the second line gives me the "Array" and message since it is encoded as an array within the json data. How can I fix this?

Here is the contents of the 'params' database field:

{"phone":"444-336-5678","birth_year":"1965","volunteer":[""],"ride_catagory":["10-12","13-15","16 plus"],"ride_note":"on","membership_visible":"on"}
  • 写回答

2条回答 默认 最新

  • douqian6315 2015-02-18 14:57
    关注

    You could manage the thing in different ways. You actual way is relying on knowing already the keys of the array you get, so I guess you can also assume that you know the type of content in it. If you know the type you can just perform a particular behaviour when you know that the content of the element will be an array as well .

    <?php
    $row = array(
        'params' => '{"phone":"444-336-5678","birth_year":"1965","volunteer":[""],"ride_catagory":["10-12","13-15","16 plus"],"ride_note":"on","membership_visible":"on"}'
    );
    $user_params = json_decode($row['params'], true);
    ?>
    

    and then something like (in a quite blindly way):

    <td><?= $user_params['phone']; ?></td>
    <td><?= implode($user_params['ride_catagory'],', '); ?></td>
    

    or like (conceptually better):

    <td><?= $user_params['phone']; ?></td>
    <td>
        <?php
            if(is_array($user_params['ride_catagory'])) {
                echo '<table><tr>'.PHP_EOL;
                foreach ($user_params['ride_catagory'] as $category) {
                    echo sprintf('<td>%s</td>'.PHP_EOL, $category);
                }
                echo '</tr></table>';
            } else {
                echo $user_params['ride_catagory'];
            }
        ?>
    </td>
    

    It all depends on you how flexible you want the code to be (like working even if you don't know what type of data an array element will contain).

    NOTE

    I would add that you shouldn't directly print value to the output if you didn't validate them (otherwise you expose yourself to XSS attacks).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。