【问题标题】:How to design an Array Structure and Encode to Json using PHP?如何使用 PHP 设计数组结构并编码为 Json?
【发布时间】:2012-08-16 07:29:03
【问题描述】:

我在 MySql 中有 2 个表

部分

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(???????);

我想在 PHP 中编码 JSON 以获得如下所示的结果:

{sections:[
{sectionName: "Section1", categoryList: [{categoryName: "category1"},
              {categoryName: "category2"}]},
{sectionName: "Section1", categoryList: [{categoryName: "category3"}]}<br>
]}

关于如何设计一个看起来像这样的数组的任何线索?

【问题讨论】:

标签: php mysql json encode


【解决方案1】:
$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"}]}}]}

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

【讨论】:

    【解决方案2】:

    这样的事情应该可以工作。

    $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 作为索引,我使用了更好的部分 ID。类别相同。

    {sections:[
    {sectionID: "SectionName", categoryList: [{categoryID: "categoryName"},
                  {categoryName: "category2"}]},
    {sectionID: "SectionName", categoryList: [{categoryID: "categoryName"}]}<br>
    ]}
    

    【讨论】:

      【解决方案3】:
      $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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-13
        • 1970-01-01
        • 1970-01-01
        • 2016-01-08
        • 2019-08-26
        • 2016-02-09
        • 2013-08-07
        • 1970-01-01
        相关资源
        最近更新 更多