从cakephp中的json数组中删除模型名称

时间:2015-03-23 20:46:31

标签: 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->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",
        }
    }
}}

如何摆脱额外的"结果"数据包装器,以便我的数据以相同的格式出现?

1 个答案:

答案 0 :(得分:0)

使用current() php函数。在你的控制器中

$results = current($results);
set()电话

之前