2013-12-08 58 views
0

我发现的Areas列表我CakePHP 2.x网站和支持,JSON输出如下面find all方法:CakePHP的JSON格式变化

$this->Area->find('all', array('fields' => array('id', 'name', 'order'), 'conditions' => array('Area.status' => 1))); 

下面是我的JSON输出:

[{"Area":{"id":"2","name":"Area 1","order":"1"}},{"Area":{"id":"3","name":"Area 2","order":"1"}}] 

现在是否可以删除每次重复的Area标签?

任何补丁相同?如果有任何建议/想法,请告诉我。

回答

0

CakePHP提供一些内置的库函数,用于从结果集中提取数据并输出与JSON格式相同的输出。

// initialize your function to render false and response type json 
    $this->autoRender = false; // no view to render 
    $this->response->type('json'); 

    // Remove Area from array and encode 
    $area = Set::extract('/Area/.', $area); 
    $json = json_encode($area); 
    $this->response->body($json); 

希望它有帮助!

0

你的输出JSON视图写:

echo json_encode(Set::extract('/Area/.', $area)); 

对我来说工作正常。