Ripwinder
Ripwinder

Reputation: 210

Illegal offset type in Zend Framework 1 when using dynamic array

I've researched illegal offset types but can't seem to get to the bottom of this one.

I have a script that selects id from table linkages column id and returning an array $resultid.

In the second part of my script I have a loop that is selecting content from latestRevision where $linktagId is equal to $id.

When I declare $id = $resultid and the $resultid has more than one value, I get Warning:

Illegal offset type ... on line 252 

line 252 :

$result[$lId] = $stmt->fetch(); 

But if i limit the values in the original array to one by changing fetchAll to Fetch it runs fine.

Any help would be much appreciated. Here is my code:

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);
}

Upvotes: 1

Views: 172

Answers (1)

doydoy44
doydoy44

Reputation: 5772

If I'm not mistaken, fetchAll will return an array of arrays.
So $lId is an array.

Try something like this :

foreach($id as $lId_all) {
    $lId = $lId_all[0];
    ....

Or

foreach($id as $lId_all) {
    foreach($lId_all as $lId) {
    ....

Upvotes: 1

Related Questions