以json格式显示php中的多维数组

时间:2017-11-16 06:02:01

标签: php mysql arrays json

我是API的初学者,想要将多维数组显示为json格式。 我在mysql中有三个表

**类别(category_id,category_name,category_description),

**子类别(subcategory_id,subcategory_name,subcategory_description,category_id,category_name),

产品(product_id,product_name,product_description,product_price category_id,subcategory_id,subcategory_name)

我想将输出显示为

--->类别1

 --->subcategory1

        ---->product1

        ---->product2

---->类别2

---->subcategory2

       ----->product1

       ----->product2

依旧......

所以请帮助做同样的事情..

我完全选择在API

中显示此输出

对于单一类别我正在使用代码

//*******View Categories From database
    private function viewcategory(){    
            // Cross validation if the request method is GET else it will return "Not Acceptable" status
            if($this->get_request_method() != "GET"){
                $this->response('',406);
            }
            $sql = mysql_query("SELECT * FROM category WHERE category_status = 1", $this->db);
            if(mysql_num_rows($sql) > 0){
                $result = array();
                while($rlt = mysql_fetch_array($sql,MYSQL_ASSOC)){
                    $result[] = $rlt;
                }
                // If success everythig is good send header as "OK" and return list of users in JSON format
                $this->response($this->json($result), 200);
            }
            $this->response('',204);    // If no records "No Content" status
        }
    //******End of View Categories Function

我想对所有类别使用相同的

0 个答案:

没有答案