[英]Echo JSON encoded data
我使用Codeigniter RESTful API根據輸入從數據庫中獲取數據。 現在,我想以其他方式更改顯示json輸出的方式。
碼:
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);
}
}
上面的代碼是我的http請求的控制器。
電流輸出
{
"Consultants": [
{
"sub_category": "Consultants"
},
{
"skills": "Career Counsellor, Creative Consultant,Digital Consultant"
},
{
"sub_category": "Accounting"
},
{
"skills": "Accountant,Auditor,Tax Specialist"
}
]
}
預期產量:
{
"Consultants": [
{
"sub_category": "Consultants",
"skills": "Career Counsellor,Creative Consultant,Digital Consultant"
},
{
.....
}
]
}
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);
}
}
使用foreach作為鍵,值,並將$ key放置在像$data[$skills['cat']][$key]['sub_category']
您將獲得所需的輸出
只需為sub_cat和技能創建單獨的臨時數組
$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);
}
}
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);
}
}
使用如下的array_push()
方法:
$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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.