Search code examples
phparrayszend-frameworkzend-db

Get values from Zend_Db_Table_Row Object array


i am using a zend model which returns me an object in form of $row with all values

but i am not able to get value from this array . is this posible to get values without foreach

this is the array returned

     Zend_Db_Table_Row Object
   (
    [_data:protected] => Array
        (
            [user_id] => 2
            [udid] => 34
            [firstname] => a
            [lastname] => a
            [email] => jusic.sl@gmail.com
            [username] => abc
            [password] => c91718531fd9f8b89c4e
            [created_date] => 2010-02-11
            [updated_datetime] => 2012-06-25 12:48:17
            [lastlogin_datetime] => 
            [group_id] => 2
            [status] => Active
        )
 )

i need to get the user_id,firstname,email from this array

any help will be appreciated .

i have tried like

$forgotpassword = $userModel->forgotpassword ( $post ); // which contains this array

$id = $forgotpassword['_data:protected']['id']; exit; // but doesnt seem to work


Solution

  • You cannot access _data directly. It's protected.

    From the ZF Reference Guide on Naming Conventions:

    […] variables that are declared with the "private" or "protected" modifier, the first character of the variable name must be a single underscore.

    You can do either do (due to __get/__set)

    echo $forgotpassword->user_id;
    

    or (due to ArrayAccess)

    echo $forgotpassword['user_id'];
    

    or (if you want an array)

    $array = $forgotpassword->toArray();
    echo $array['user_id'];
    

    Please see the Reference Guide and the code