Re: Newbie: Array of objects iteration

From: Date: Fri, 09 Oct 2009 06:13:12 +0000
Subject: Re: Newbie: Array of objects iteration
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-298788@lists.php.net to get a copy of this message
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 <talofo@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

« previous php.general (#298788) next »