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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?