Reputation: 814
Im using Codeigniter RESTful API to get the data from from database based on the input. Now I want to change the way of displaying my json output in different way.
CODE:
function categoryskills_get()
{
$category = $this->get('category');
$skills = $this->Users_model->categoryskills($category);
if($skills != FALSE)
{
$sub_cat = 0;
foreach($skills['skills'] as $row)
{
$data[$skills['cat']][]['sub_category'] = $row['sub_cat'];
$data[$skills['cat']][]['skills'] = $row['s_name'];
}
$this->set_response($data, REST_Controller::HTTP_OK);
}else{
$response["error"] = TRUE;
$response["status"] = '404';
$response["error_msg"] = "Category not found!";
$this->set_response($response, REST_Controller::HTTP_NOT_FOUND);
}
}
Above code is the controller for my http request.
Current output:
{
"Consultants": [
{
"sub_category": "Consultants"
},
{
"skills": "Career Counsellor, Creative Consultant,Digital Consultant"
},
{
"sub_category": "Accounting"
},
{
"skills": "Accountant,Auditor,Tax Specialist"
}
]
}
Expected output:
{
"Consultants": [
{
"sub_category": "Consultants",
"skills": "Career Counsellor,Creative Consultant,Digital Consultant"
},
{
.....
}
]
}
Upvotes: 0
Views: 271
Reputation: 52
function categoryskills_get()
{
$category = $this->get('category');
$skills = $this->Users_model->categoryskills($category);
if($skills != FALSE)
{
$sub_cat = 0;
foreach($skills['skills'] as $row)
{
$data[$skills['cat']][] = array('sub_category'=>$row['sub_cat'],'skills'=>$row['s_name']);
}
$this->set_response($data, REST_Controller::HTTP_OK);
}else{
$response["error"] = TRUE;
$response["status"] = '404';
$response["error_msg"] = "Category not found!";
$this->set_response($response, REST_Controller::HTTP_NOT_FOUND);
}
}
Upvotes: 1
Reputation: 662
just make seperate temp array for sub_cat, and skills
$category = $this->get('category');
$skills = $this->Users_model->categoryskills($category);
$data = array();//create an empty array
if($skills != FALSE)
{
$sub_cat = 0;
foreach($skills['skills'] as $row)
{
$arr['sub_category'] = $row['sub_cat'];
$arr['skills'] = $row['s_name'];//makes an $arr array
$data[$skills['cat']][] = $arr; //and after that add it to main array
}
$this->set_response($data, REST_Controller::HTTP_OK);
}else{
$response["error"] = TRUE;
$response["status"] = '404';
$response["error_msg"] = "Category not found!";
$this->set_response($response, REST_Controller::HTTP_NOT_FOUND);
}
}
Upvotes: 2
Reputation: 6994
use array_push()
method like below:
$category = $this->get('category');
$skills = $this->Users_model->categoryskills($category);
$data = array();//create an empty array
if($skills != FALSE)
{
$sub_cat = 0;
foreach($skills['skills'] as $row)
{
$arr['sub_category'] = $row['sub_cat'];
$arr['skills'] = $row['s_name'];//makes an $arr array
array_push($data[$skills['cat']], $arr);//push array
}
$this->set_response($data, REST_Controller::HTTP_OK);
}else{
$response["error"] = TRUE;
$response["status"] = '404';
$response["error_msg"] = "Category not found!";
$this->set_response($response, REST_Controller::HTTP_NOT_FOUND);
}
}
Upvotes: 0
Reputation: 340
function categoryskills_get()
{
$category = $this->get('category');
$skills = $this->Users_model->categoryskills($category);
if($skills != FALSE)
{
$sub_cat = 0;
foreach($skills['skills'] as $key=>$row)
{
$data[$skills['cat']][$key]['sub_category'] = $row['sub_cat'];
$data[$skills['cat']][$key]['skills'] = $row['s_name'];
}
$this->set_response($data, REST_Controller::HTTP_OK);
}else{
$response["error"] = TRUE;
$response["status"] = '404';
$response["error_msg"] = "Category not found!";
$this->set_response($response, REST_Controller::HTTP_NOT_FOUND);
}
}
Use foreach as key,value and placed $key into array like this $data[$skills['cat']][$key]['sub_category']
you will get desired output
Upvotes: 2