从cakephp中的json数组中删除模型名
我已经在cakephp控制器中设置了自定义搜索操作从cakephp中的json数组中删除模型名,php,arrays,json,cakephp,Php,Arrays,Json,Cakephp,我已经在cakephp控制器中设置了自定义搜索操作 public function search() { $results = []; $results['data'] = $this->Content->find('first', array( 'conditions' => array($this->request->params['pass'][0] . ' like' => "%" . $this->request-&
public function search() {
$results = [];
$results['data'] = $this->Content->find('first',
array(
'conditions' => array($this->request->params['pass'][0] . ' like' => "%" . $this->request->params['pass'][1] . "%")), array('contain' => false)
);
if(count($results['data'])>0){
$results['success'] = true;
$this->set(compact('results'));
$this->set('_serialize', array('results'));
}
else{
$results['success'] = false;
}
}
我遇到的问题是,我的API的其余部分将数据格式化如下:
{
"success": true,
"data": [
{
"id": "5509be6c-9ef8-42c3-af39-2d492773a233",
"title": "test2",
},
{
"id": "5509be6c-9ef8-42c3-af39-2d492773a233",
"title": "test1"
}
]
}
但我现在从cakephp获得的搜索操作是:
{
"results": {
"success": true,
"data": {
"Content": {
"id": "52efcbeb-e984-4a2e-b76f-0cc34056922c",
"title": "Homeasdfasdf",
}
}
}}
如何去掉额外的“results”数组包装器,使数据以相同的格式显示?使用php函数。在你的控制器里
$results = current($results);
在调用set()
之前