我使用Codeigniter RESTful API根据输入从数据库中获取数据。现在我想以不同的方式改变显示我的json输出的方式。
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);
}
}
上面的代码是我的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"
},
{
.....
}
]
}
答案 0 :(得分:2)
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']
你会得到理想的输出
答案 1 :(得分:2)
为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);
}
}
答案 2 :(得分:1)
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);
}
}
答案 3 :(得分:0)
使用如下所示的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);
}
}