php - PHP 中两个 mysql 表的 Json 需要按层次结构输出

标签 php mysql arrays json

我有两张 table ,

1 - 类别

2 - 子类别

 Category (table 1)
---------------
Cid    |   Name

1      |   Vegetable

2      |   Fruit


SubCategory  (table 2)
----------------
Cid  |  Sid | S_name

 1   |   1  | Carrot

 1   |   2  | Beans

 2   |  3   | Mango

我想要 Json 中的结果:-

  -> Category found

       ->  Vegetable

            -> carrot

             -> beans

       ->  Fruit

             -> mango

我正在使用下面的代码!任何人都可以帮助我纠正我的代码并获得像我上面提到的那样的输出!我尝试了很多次。但没有得到解决方案!请帮助我获得类似目录形式的输出。谢谢

`$srtResult = "SELECT * FROM `category` `c` LEFT JOIN `subcategory` `s` ON 
(`c`.`cid` = `s`.`cid`)";

 //Execute Qu**strong text**ery
    $result=mysql_query($srtResult);
   //Iterate Throught The Results

  while ($row = mysql_fetch_assoc($result, MYSQL_ASSOC)) 
{ $count = $row['cid'];
 while($row['cid'] = $count)
 { $subcatitem[] = Array( "cid" => $row['cid'], "sid" =>$row['sid'],          
"sname" => $row['sc_name'] );
  }
  $json['category'][] = Array("category" => Array( "cid" => $row['cid'], 
"name" => $row['name'], "subcategory" => Array( $subcatitem))); 
  } 

  header('Content-type: application/json');
echo json_encode($json);`   

最佳答案

这还不够吗?

$srtResult = "SELECT * FROM `category` `c` LEFT JOIN `subcategory` `s` ON 
(`c`.`cid` = `s`.`cid`)";
$result = mysql_query($srtResult);

while ($row = mysql_fetch_assoc($result, MYSQL_ASSOC)) {
  $json['category'][$row['name']] = $row['sc_name']; 
} 

header('Content-type: application/json');
echo json_encode($json);

编辑:

在注释中进行更多解释后,所需的 PHP 数组结构(给出预期的 JSON 字符串)应如下所示:

Array
(
    [success] => 1
    [message] => Category found in Database
    [Category] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => vegetable
                    [Subcategory] => Array
                        (
                            [0] => Array
                                (
                                    [cid] => 1
                                    [sid] => 1
                                    [sc_name] => carrot
                                )

                            [1] => Array
                                (
                                    [cid] => 1
                                    [sid] => 2
                                    [sc_name] => beans
                                )

                        )

                )

            [1] => Array
                (
                    [id] => 2
                    [name] => Fruit
                    [Subcategory] => Array
                        (
                            [0] => Array
                                (
                                    [cid] => 1
                                    [sid] => 1
                                    [sc_name] => Mango
                                )

                        )

                )

        )

)

关于php - PHP 中两个 mysql 表的 Json 需要按层次结构输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43860005/

相关文章:

php - 在不发送电子邮件且不在 PHP 中使用 pear 的情况下检查 SMTP 身份验证

php - 将数组参数从 Controller 传递到 Mailable 类。拉维

php - 如何在列中列出 mysql 聚合函数的项目

php - 如何使用php分解字符串,去除多余字符

c++ - 我可以引用初始值设定项列表的先前成员吗?

javascript - 试图获得每个对象的总和

javascript - 使用 javascript 验证密码

Php mysqli 选择位置和顺序

php - 从更新链接更新 mySQL

php - 如何将 PostgreSql 与 PHP 结合起来?