How to add "key" and "value" to json/array Rest Api in php

Viewed 100

I am working with Rest Api using Codeigniter, Iwant to return "two arrays" (FilterType='Tag' OR FilterType='Merchant'),I am getting successfully but i want to "FilterId" also (static or dynamic) I am getting response in json and i want to append "FilterId" in array/json but not in loop , i want to put "FilterId" below "data" (in json response),how can i do this ?

Here is my controller

$CategoryUrl = trim($this->input->post('CategoryUrl'));
$users = $this->mymodel->SearchRecords($CategoryUrl);

$responseJSON = array(data" => $users);
header("content-type:application/json");
$response = json_encode($responseJSON);
echo $response;

Here is my model code

function SearchTagRecords($CategoryUrl)
    {
        $this->db->select("fp.id,fp.CategoryUrl,fp.FilterType,fp.FilterName,fp.paramId,fp.ParamName,fp.CategoryName")
        ->from("filterproducts  fp")
        ->join("filterid fi", "fp.FilterType=fi.name")
        ->where("fp.CategoryUrl", $CategoryUrl);
        $query = $this->db->get();  
        
        $result = $query->result_array();
        $tags=array();
        $merchant=array();
        foreach($result as $rec)
            {
                if($rec['FilterType']=="Tag")
                    {
                         $tags[]=$rec;
                    }else if($rec['FilterType']=="Merchant")
                    {
                        $merchant[]=$rec;
                    }                 
           }
            $final=[];
            $final['tags'] = $tags;
            $final['merchant'] = $merchant;
            return $final;
        }

Right now here is my json output

{
    "Status": "1",
    "data": {
        "tags": [
            {
                "id": "23239",
                "CategoryUrl": "restaurants",
                "FilterType": "Tag",
                "FilterName": "collection",
                "paramId": "2",
                "ParamName": "Buffet",
                "CategoryName": "Restaurants & Food "
            },
        ......And so on
           "merchant": [
            {
                "id": "23250",
                "CategoryUrl": "restaurants",
                "FilterType": "Merchant",
                "FilterName": "brand",
                "paramId": "12372",
                "ParamName": "Peddlers",
                "CategoryName": "Restaurants & Food "
            },
        ....And so on   
        ]
    }
}

I want json output like following one (Add "FilterId"(coming from db or we can put static also) in json ) How can i do this ?

{
    "Status": "1",
    "data": {
    "FilterId": "1",
        "tags": [
            {
                "id": "23239",
                "CategoryUrl": "restaurants",
                "FilterType": "Tag",
                "FilterName": "collection",
                "paramId": "2",
                "ParamName": "Buffet",
                "CategoryName": "Restaurants & Food "
            },
        ......And so on
        "FilterId": "2",
           "merchant": [
            {
                "id": "23250",
                "CategoryUrl": "restaurants",
                "FilterType": "Merchant",
                "FilterName": "brand",
                "paramId": "12372",
                "ParamName": "Peddlers",
                "CategoryName": "Restaurants & Food "
            },
        ....And so on   
        ]
    }
}
0 Answers
Related