如何使用PHP设计数组结构并编码为Json?

如何使用PHP设计数组结构并编码为Json?,php,mysql,json,encode,Php,Mysql,Json,Encode,我在MySql中有两个表 部分 ID Name ===================== 1 Section1 2 Section2 类别 ID SectionID Name ========================================= 1 1 Category1 2 1 Category2 3 2

我在MySql中有两个表

部分

ID       Name
=====================
1        Section1
2        Section2
类别

ID        SectionID     Name
=========================================
1           1           Category1
2           1           Category2
3           2           Category3
这就是我现在拥有的:

$sql_section = "select * from section";<br>
$sql_category = "select * from category";<br>
$result_section = mysql_query($sql_section) or die("Could not execute query.");
$result_category = mysql_query($sql_category) or die("Could not execute query.");

echo json_encode(???????);
$sql\u section=“选择*部分”
$sql\u category=“从类别中选择*”
$result\u section=mysql\u query($sql\u section)或die(“无法执行查询”); $result\u category=mysql\u query($sql\u category)或die(“无法执行查询”); echo json_编码(?);
我想用PHP对JSON进行编码,得到如下结果:

{sections:[
{sectionName: "Section1", categoryList: [{categoryName: "category1"},
              {categoryName: "category2"}]},
{sectionName: "Section1", categoryList: [{categoryName: "category3"}]}<br>
]}
{部分:[
{sectionName:“Section1”,类别列表:[{categoryName:“category1”},
{categoryName:“category2”}]},
{sectionName:“Section1”,类别列表:[{categoryName:“category3”}]}
]}
关于如何设计这样的数组,有什么线索吗

$arr = array('sections' => array());
$arr['sections'][] = array('sectionName' => array('categoryList' => array( array('categoryName' => 'Category 1'), array('categoryName' => 'Category 2'))));
$arr['sections'][] = array('sectionName' => array('categoryList' => array( array('categoryName' => 'Category 3'), array('categoryName' => 'Category 4'))));
echo json_encode($arr);
输出://

{"sections":[
   {"sectionName":
      {"categoryList":
         [{"categoryName":"Category 1"},
          {"categoryName":"Category 2"}]}
      },
    {"sectionName":
      {"categoryList":
         [{"categoryName":"Category 3"},{"categoryName":"Category 4"}]}}]}

您只需将字符串值替换为变量,并将其放入循环中,即可创建所需的数据集。

类似的方法应该可以工作

$sections = mysql_query("select * from section") or die("Could not execute query.");
$result = array();
if(mysql_num_rows($sections)>0) {
    while($section = mysql_fetch_assoc($sections))   {
        $result['sections'][$section['ID']] = $section['Name'];
        $categories = mysql_query("select * from category where SectionID='".mysql_real_escape_string($section['ID'])."'");
        if(mysql_num_rows($categories)>0) {
            while($category = mysql_fetch_assoc($categories))  {
                    $result['sections'][$section['ID']]['categoryList'][$category['ID']] = $category['Name']; 
            }
        }
    }
}

echo json_encode($result);
它将输出如下,而不是sectionName作为索引,我使用了SectionID,这更好。类别也一样

{sections:[
{sectionID: "SectionName", categoryList: [{categoryID: "categoryName"},
              {categoryName: "category2"}]},
{sectionID: "SectionName", categoryList: [{categoryID: "categoryName"}]}<br>
]}
{部分:[
{sectionID:“SectionName”,类别列表:[{categoryID:“categoryName”},
{categoryName:“category2”}]},
{sectionID:“SectionName”,类别列表:[{categoryID:“categoryName”}]}
]}
请不要使用不推荐使用的mysql\u XXX函数,请使用mysqli或PDO。为什么?有什么建议读的文章吗
$sections = array();
$categories = array();
while ($row = mysql_fetch_object($result_section))
  $sections[$row->ID] = array('sectionName' => $row->Name, 'categoryList' => array());
while ($row = mysql_fetch_object($result_category))
  $sections[$row->sectionID]['categoryList'][] = array('categoryName' => $row->Name);