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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记