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

数组的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).

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doujuncuo9339 2015-02-18 14:46

    Try to join the values

    <td>{$user_params['phone']}</td>
    <td>{implode($user_params['ride_catagory'],',')}</td>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题