返回json_object作为数组的正确方法 [英] correct way to return a json_object as an array

查看:121
本文介绍了返回json_object作为数组的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以以下方式使用sql语句填充的数组:

I have an array populated using an sql statement in the following manner:

$index = 0;

while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
    $bookname[$index] = ($row['Bookname']);
    $subjectname[$index] = ($row['SubjectName']);
    $index++;
}

当我回显json编码数组时,我得到一个空白[]我知道这已经很奇怪了。
我在上下文中做错了什么

When I go to echo json encode the Arrays I get a blank [] when I know it has been populated which is really weird. Am I doing anything wrong in my context

echo json_encode($Bookname,$SubjectName);


推荐答案

您可以使用 json_encode 像这样:

<?php
$index = 0;
$data = array();
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
    $data[$index]['bookname'] = $row['Bookname'];
    $data[$index]['subjectname'] = $row['SubjectName'];
    $index++;
}
json_encode($data); // encode your array
?>

这篇关于返回json_object作为数组的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆