1

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

1 Answer 1

1

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) {
    ....
Sign up to request clarification or add additional context in comments.

4 Comments

fyi, another way of 'losing' the 'outer' array, which is only one entry, is to use 'current($lId)'. This will return the 'inner' array. i.e. foreach(current($lId) as $id ); It takes advantage of the the 'inbult array iterator' that all 'arrays' have.
@doydoy44 You are the man! Thats twice in one day you have helped me, thank you very much. It was the second suggestion that worked.
@Ryan Vincent - I tried this suggestion, it removed the error but would only return one result. Thank you for helping.
sorry about that. - i should have tested it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.