如何以JSON格式制作类别明智的产品

时间:2018-02-07 10:18:25

标签: php arrays json

我有一个阵列。在该数组中categoryName意味着我们必须创建多维数组,否则我们必须生成普通数组。我试过,但我无法做出我期望的JSON格式。

  

的print_r($ search_result);

Array
(
    [0] => Array
        (
            [productId] => 1
            [categoryName] => Shoes
            [brandName] => Adidas
        )

    [1] => Array
        (
            [productId] => 2
            [categoryName] => Jeans
            [brandName] => LEVIS
        )

    [2] => Array
        (
            [productId] => 3
            [categoryName] => Jeans
            [brandName] => LEVIS
        )

)

使用这个数组我想做这样的JSON格式

    {
  "status": "Success",
  "data": [
    {
      "categoryName": "Shoes",
      "products": [
        {
          "productId": "1",
          "brandName": "Adidas"
        }
      ]
    },
    {
      "categoryName": "Jeans",
      "products": [
        {
          "productId": "2",
          "brandName": "LEVIS"
        },
        {
          "productId": "2",
          "brandName": "LEVIS"
        }
      ]
    }
  ]
}

为了超越我的预期结果,我尝试了这样但是没有发生。我正在获得json格式

protected function getCategorywiseProducts(){
        if($this->get_request_method() != "GET"){
            $this->response('',406);
        }
        $sql = "SELECT productId,categoryName,brandName FROM product";
        $mainArray = array();
        $search_result = $this->GetJoinRecord($sql);
        $tasks = array();
        foreach ($search_result as $key => $value) {
            $tasks[$value['categoryName']][] = $value;
        }
        if(count($tasks)>0) {
            $response_array['status']='success';
            $response_array['categories'][]=$tasks;
            $this->response($this->json($response_array), 200);
        } else {
            $response_array['status']='fail';
            $response_array['message']='Record not found.';
            $response_array['data']='';
            $this->response($this->json($response_array), 204);
        }
    }

我变得像JSON格式

    {
    "status": "success",
    "categories": [
        {
            "Shoes": [
                {
                    "productId": "1",
                    "categoryName": "Shoes",
                    "brandName": "Adidas"
                }
            ],
            "Jeans": [
                {
                    "productId": "2",
                    "categoryName": "Jeans",
                    "brandName": "LEVIS"
                },
                {
                    "productId": "3",
                    "categoryName": "Jeans",
                    "brandName": "LEVIS"
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

尝试修改这样的功能:

protected function getCategorywiseProducts(){
    if($this->get_request_method() != "GET"){
        $this->response('',406);
    }
    $sql = "SELECT productId,categoryName,brandName FROM product";
    $mainArray = array();
    $search_result = $this->GetJoinRecord($sql);
    $tasks = array();
    foreach ($search_result as $key => $value) {
        $tasks['categoryName'][$key] = $value['categoryName'];
        $tasks['products']['productId'][$key] = $value['productId'];
        $tasks['products']['brandName'][$key] = $value['brandName'];
    }
    if(count($tasks)>0) {
        $response_array['status']='success';
        $response_array['categories'][]=$tasks;
        $this->response($this->json($response_array), 200);
    } else {
        $response_array['status']='fail';
        $response_array['message']='Record not found.';
        $response_array['data']='';
        $this->response($this->json($response_array), 204);
    }
}

如您所见,我只更改了foreach循环。