使用动态数组时 Zend Framework 1 中的非法偏移类型
Illegal offset type in Zend Framework 1 when using dynamic array
我研究过非法偏移类型,但似乎无法深入了解这一点。
我有一个脚本,它从 table linkages
列 id
中选择 id
并返回一个数组 $resultid
.
在脚本的第二部分,我有一个循环,它从 latestRevision
中选择 content
,其中 $linktagId is equal to $id
。
当我声明 $id = $resultid
并且 $resultid 有多个值时,我收到警告:
Illegal offset type ... on line 252
第 252 行:
$result[$lId] = $stmt->fetch();
但是如果我通过将 fetchAll 更改为 Fetch 将原始数组中的值限制为 1,它运行正常。
如有任何帮助,我们将不胜感激。这是我的代码:
public function testAction()
{
//Return list of tags for the defined user and make default type 10
$u = 2;
$t = 10;
$resultid = array();
//Connect to database
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select()
->from(array('lt' => 'Linktags'),
array('id'))
->where('userId = ?', $u)
->where('type = ?', $t);
$stmt = $select->query();
$resultid = $stmt->fetchAll();
//print_r($resultid);
//Get latest revision from database and loop through $id's
$id = $resultid;
//print_r($id);
//setup array and loop
$result = array();
foreach($id as $lId) {
//Connect to database
$db = Zend_Db_Table::getDefaultAdapter();
//Perform Query
$select = $db->select('')
->from(array('lr'=>'LinktagRevisions'),
array('content'))
->where('linktagId = ?', $lId)
->order('updated DESC')
->limit(1);
$stmt = $select->query();
$result[$lId] = $stmt->fetch();
}
$this->_helper->json($result,true);
}
如果我没记错的话,fetchAll
会 return 一个数组的数组。
所以$lId
是一个数组。
尝试这样的事情:
foreach($id as $lId_all) {
$lId = $lId_all[0];
....
或
foreach($id as $lId_all) {
foreach($lId_all as $lId) {
....
我研究过非法偏移类型,但似乎无法深入了解这一点。
我有一个脚本,它从 table linkages
列 id
中选择 id
并返回一个数组 $resultid
.
在脚本的第二部分,我有一个循环,它从 latestRevision
中选择 content
,其中 $linktagId is equal to $id
。
当我声明 $id = $resultid
并且 $resultid 有多个值时,我收到警告:
Illegal offset type ... on line 252
第 252 行:
$result[$lId] = $stmt->fetch();
但是如果我通过将 fetchAll 更改为 Fetch 将原始数组中的值限制为 1,它运行正常。
如有任何帮助,我们将不胜感激。这是我的代码:
public function testAction()
{
//Return list of tags for the defined user and make default type 10
$u = 2;
$t = 10;
$resultid = array();
//Connect to database
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select()
->from(array('lt' => 'Linktags'),
array('id'))
->where('userId = ?', $u)
->where('type = ?', $t);
$stmt = $select->query();
$resultid = $stmt->fetchAll();
//print_r($resultid);
//Get latest revision from database and loop through $id's
$id = $resultid;
//print_r($id);
//setup array and loop
$result = array();
foreach($id as $lId) {
//Connect to database
$db = Zend_Db_Table::getDefaultAdapter();
//Perform Query
$select = $db->select('')
->from(array('lr'=>'LinktagRevisions'),
array('content'))
->where('linktagId = ?', $lId)
->order('updated DESC')
->limit(1);
$stmt = $select->query();
$result[$lId] = $stmt->fetch();
}
$this->_helper->json($result,true);
}
如果我没记错的话,fetchAll
会 return 一个数组的数组。
所以$lId
是一个数组。
尝试这样的事情:
foreach($id as $lId_all) {
$lId = $lId_all[0];
....
或
foreach($id as $lId_all) {
foreach($lId_all as $lId) {
....