I think you are just looking for the key in the wrong place. Try:

foreach ( $records as $record ) {            
    foreach( $record as $column=>$value ) {
        echo "$column is $value\n";
    }
}

You've got the columns names in each record, not in the global recorset.

Good luck ;-)




________________________________
De: MEM <tal...@gmail.com>
Para: php-general@lists.php.net
Enviado: jue,8 octubre, 2009 22:59
Asunto: [PHP] Newbie: Array of objects iteration

Hello all,

I'm grabbing all records from a table using:

$records = $stmt->fetchAll(PDO::FETCH_OBJ);
return $records;


In order to display the values we can do:


foreach ($records as $record)
{            
        echo $record->id;
        echo $record->name;
}


However, I'd like to grab, also, the *column names*.

I've tried:

foreach ($records as $column=>$value)
{
    echo "$column is $value\n";
}

But I get:
"Catchable fatal error: Object of class stdClass could not be converted to
string"



Can I have your help on how can I properly get the column values?

Regards,
Márcio


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


      

Reply via email to