我尝试创建json对象,但无法创建

|| 在这里我有一些功能可以读取我的桌子
<?php
 public function get_all_record($table, $fields = \"*\"){

            $sql = \"SELECT $fields FROM $table\";

            $result = $this->sqlordie($sql);

            $xx=0;
            while($row = mysql_fetch_assoc($result))
              {
                $myrow[$xx] = $row;
                $xx++;
              }


             mysql_free_result($result);
             return $myrow;

        }
     private function sqlordie($sql) {

            $return_result = mysql_query($sql, $this->conn);
            if($return_result) {
                return $return_result;
            } else {
                $this->sql_error($sql);
            }
        }

        private function sql_error($sql) {
            echo mysql_error($this->conn).\'<br>\';
            die(\'error: \'. $sql);
        }
?>
下面的代码我调用get_all_record函数并返回结果,我使用json_encode转换为json对象
 <?php
 $myItem = get_all_record(\"mc_category\",\"category_id,category_name,category_description\");

    echo json_encode($myItem);
?>
我得到如下输出
[{\"category_id\":\"2\",\"category_name\":\"book\",\"category_description\":\"all type of books\"},{\"category_id\":\"3\",\"category_name\":\"book\",\"category_description\":\"all type of books\"},{\"category_id\":\"4\",\"category_name\":\"Phone\",\"category_description\":\"All type of phones\"},{\"category_id\":\"5\",\"category_name\":\"Phone\",\"category_description\":\"All type of phones\"}]
但是我需要如下的JSON对象
{ \"aaData\": [
    [\"Trident\",\"Internet Explorer 4.0\",\"Win 95+\",\"4\",\"X\"],
    [\"Trident\",\"Internet Explorer 5.0\",\"Win 95+\",\"5\",\"C\"],
    [\"Trident\",\"Internet Explorer 5.5\",\"Win 95+\",\"5.5\",\"A\"],
    [\"Trident\",\"Internet Explorer 6\",\"Win 98+\",\"6\",\"A\"],
    [\"Trident\",\"Internet Explorer 7\",\"Win XP SP2+\",\"7\",\"A\"]
] }
您能帮忙创建上述json对象吗?
已邀请:
json_encode获取具有名称/值对的数组。您只需要这些值。 最好的解决方案是使用 mysql_fetch_row($ result) 获取数字数组,该数组将转换为您首选的json格式。 如果以后需要某个名称/值对,仍可以使用 mysql_fetch_assoc($ result) 并以这种方式创建第二个数组: $ mySoonToBeJsonData = array_values($ myrow); 这将从关联数组中删除名称
尝试使用以下代码进行编码:
echo json_encode($myItem, true);

要回复问题请先登录注册