De: MEM <tal...@gmail.com>
Para: Lester Caine <les...@lsces.co.uk>; php-general@lists.php.net
Enviado: vie,9 octubre, 2009 12:40
Asunto: RE: [PHP] Newbie: Array of objects iteration

@LinuxManMikeC and all @All
Thanks. I was more or less aware of that possibility, however, please let me
share the big picture will you guys: 

The main point is to access the properties name as well as values, of that
object, and put them on a xls file.
Instead of using mysql_num_rows, and mysql_num_files, and split and use a
bunch of loops.... I thought: 
Maybe fetching as an object may help me doing this far better. 
However, I'm having a hard time figuring out how to make the switch. 

To be honest, I don't really know if iterate is the "buzz word" here, I
would like to understand a little bit more the following:

We have in our hands an Object returned by a Fetch_All w/ fetch_obj option
applied to it, that object, when I do var_dump, reveals himself as in
object, containing an array and, each key of that array corresponds the
column names of our table or tables we have previously fetched. 
However, since we are working with an object, should we think differently
than if we were working with an array? If so, it is, in the fact, iteration,
the best option we have to put all properties and values into a xls file? Or
there are far better options for that?

Yes, actually, I was having only one record on the database, but I believe
that is far to less. Since I was not been able to see if I was getting one
object, or several objects. That make me think of that. Thanks :) And
because of that, I was able (I hope) to properly understand Fernando

foreach ( $records as $record ) {            
    foreach( $record as $column=>$value ) {
        echo "$column is $value\n";
So the first foreach will iterate over each object of stdClass that
corresponds to each record of mysql data fetched, then, for each of them,
take the associative array key and the corresponding value...
Correct? :DDD

However, is this the right track to archive the goal stated above, or, they
are far better ways for doing so?



Correct about my example, although I'm not sure if you get each record as an 
array or as an object. Anyway, you can iterate both.

And if you want to get the column names, I suppose you could use array_keys() 
on the first record if you receive an array, or maybe get_object_vars() if it's 
an objecs (note that with this you get an array with keys and values).

I've found PDOStatement->getColumnMeta(). There's a big fat warning saying this 
is experimental, but you could take a look in case you need more data on the 

Hope it helps.


Reply via email to