我想知道它是否是通过调用getTable('tbl','module')或对象返回的数组。当我尝试将返回的值插入另一个数组然后将其编码为JSON时,我遇到了问题。将其本身传递给$this->_helper->json()
会成功生成JSON输出,但是将其用作另一个数组的一部分将无法正常工作,并且是空数组。参见下面的控制器代码:
//put your code here
public function indexAction () {
}
public function browseAction () {
$resultArray = $this->prepareArray();
$this->sendResponse($resultArray);
}
/**
* HTTP Response Codes
*/
const HTTP_OK = 200;
public function sendResponse($data) {
ob_clean();
$this->_helper->json($data);
}
public function prepareArray() {
//Here we will prepare the array to be later encoded as JSON
$responseArray = [
'status_code' => '',
'body' => [
'itemsCount' => '',
'foods' => [
'food_id' => '',
'food_title' => '',
'image' => '',
],
],
];
$foodTable = Engine_Api::_()->getDbTable('foods', 'restapi');
$foods = $foodTable->getFoods($this->view);
$foodArray = [];
print_r($foods);
while ($row = mysqli_fetch_row($foods)) {
$foodArray['food_id'] = $row['food_id'];
$foodArray['food_title'] = $row['title'];
$foodArray['image'] = $row['image'];
}
error_log( $row ['food_id'] . ',' . $row['title']);
$responseArray['status_code'] = '200';
$responseArray['body']['itemsCount'] = count($foods);
$responseArray['body']['foods']= $foodsArray;
return $responseArray;
}
答案 0 :(得分:1)
如果所有设置均正确,则getTable('tbl', 'module')
返回object(Module_Model_DbTable_Tbl)
。这是数据库表的模型。
您要分配给foodsArray
未在任何地方定义的变量的行上是您的特定错误。您应该在这里分配foodArray
。
$responseArray['body']['foods']= $foodsArray; // should be foodArray