Newbie: Array of objects iteration

Newbie: Array of objects iteration

am 08.10.2009 22:59:32 von talofo talofo

Hello all,

I'm grabbing all records from a table using:

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


In order to display the values we can do:


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


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

I've tried:

foreach ($records as $column=3D>$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=E1rcio


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

Re: Newbie: Array of objects iteration

am 09.10.2009 08:13:12 von Fernando Castillo Aparicio

--0-1508936651-1255068792=:48565
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

I think you are just looking for the key in the wrong place. Try: fore=
ach ( $records as $record ) { =0A foreach( $record as $column=
=3D>$value ) {=0A echo "$column is $value\n";=0A }=0A} You'v=
e got the columns names in each record, not in the global recorset. Go=
od luck ;-) =0A________________________________=0ADe: MEM o@gmail.com>=0APara: php-general@lists.php.net=0AEnviado: jue,8 octubre, 20=
09 22:59=0AAsunto: [PHP] Newbie: Array of objects iteration Hello all,=
I'm grabbing all records from a table using: $records =3D $stmt-=
>fetchAll(PDO::FETCH_OBJ);=0Areturn $records; =0AIn order to display t=
he values we can do: =0Aforeach ($records as $record)=0A{ =
=0A echo $record->id;=0A echo $record->name;=0A} =0AHowe=
ver, I'd like to grab, also, the *column names*. I've tried: fore=
ach ($records as $column=3D>$value)=0A{=0A echo "$column is $value\n";=
=0A} But I get:=0A"Catchable fatal error: Object of class stdClass cou=
ld not be converted to=0Astring" Can I have your help on how can=
I properly get the column values? Regards,=0AM=E1rcio =0A--=0APH=
P General Mailing List (http://www.php.net/)=0ATo unsubscribe, visit: http:=
//www.php.net/unsub.php =0A
--0-1508936651-1255068792=:48565--

Re: Newbie: Array of objects iteration

am 09.10.2009 08:23:09 von Lester Caine

Fernando Castillo Aparicio wrote:
> 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.

print_r($record); often helps - you can see what is actually returned
and check that you have names rather than numbers for the fields ;)

print_r($records); becomes a little large if you have a lot of results
so use with care :)

--
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk//
Firebird - http://www.firebirdsql.org/index.php

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

RE: Newbie: Array of objects iteration

am 09.10.2009 12:40:42 von talofo talofo

@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:=20

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:=20
Maybe fetching as an object may help me doing this far better.=20
However, I'm having a hard time figuring out how to make the switch.=20


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.=20
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?



@Lester
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
example:



@Fernando
foreach ( $records as $record ) { =20
foreach( $record as $column=3D>$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?



Regards,
M=E1rcio



> -----Original Message-----
> From: Lester Caine [mailto:lester@lsces.co.uk]
> Sent: sexta-feira, 9 de Outubro de 2009 07:23
> To: php-general@lists.php.net
> Subject: Re: [PHP] Newbie: Array of objects iteration
>=20
> Fernando Castillo Aparicio wrote:
> > I think you are just looking for the key in the wrong place. Try:
> >
> > foreach ( $records as $record ) {
> > foreach( $record as $column=3D>$value ) {
> > echo "$column is $value\n";
> > }
> > }
> >
> > You've got the columns names in each record, not in the global
> recorset.
>=20
> print_r($record); often helps - you can see what is actually returned
> and check that you have names rather than numbers for the fields ;)
>=20
> print_r($records); becomes a little large if you have a lot of results
> so use with care :)
>=20
> --
> Lester Caine - G8HFL
> -----------------------------
> Contact - http://lsces.co.uk/wiki/?page=3Dcontact
> L.S.Caine Electronic Services - http://lsces.co.uk
> EnquirySolve - http://enquirysolve.com/
> Model Engineers Digital Workshop - http://medw.co.uk//
> Firebird - http://www.firebirdsql.org/index.php
>=20
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php


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

Re: Newbie: Array of objects iteration

am 09.10.2009 13:41:11 von Fernando Castillo Aparicio

--0-371926515-1255088471=:10597
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

=0A________________________________=0ADe: MEM >=0APara: Lester Caine ; php-general@lists.php.net=0AEn=
viado: vie,9 octubre, 2009 12:40=0AAsunto: RE: [PHP] Newbie: Array of objec=
ts iteration @LinuxManMikeC and all @All=0AThanks. I was more or less =
aware of that possibility, however, please let me=0Ashare the big picture w=
ill you guys: The main point is to access the properties name as well=
as values, of that=0Aobject, and put them on a xls file.=0AInstead of usin=
g mysql_num_rows, and mysql_num_files, and split and use a=0Abunch of loops=
..... I thought: =0AMaybe fetching as an object may help me doing this far b=
etter. =0AHowever, I'm having a hard time figuring out how to make the swit=
ch. =0ATo be honest, I don't really know if iterate is the "buzz word=
" here, I=0Awould 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=
=0Aapplied to it, that object, when I do var_dump, reveals himself as in=0A=
object, containing an array and, each key of that array corresponds the=0Ac=
olumn names of our table or tables we have previously fetched. =0AHowever, =
since we are working with an object, should we think differently=0Athan if =
we were working with an array? If so, it is, in the fact, iteration,=0Athe =
best option we have to put all properties and values into a xls file? Or=0A=
there are far better options for that? @Lester=0AYes, actually, =
I was having only one record on the database, but I believe=0Athat is far t=
o less. Since I was not been able to see if I was getting one=0Aobject, or =
several objects. That make me think of that. Thanks :) And=0Abecause of tha=
t, I was able (I hope) to properly understand Fernando=0Aexample: =0A=
=0A@Fernando=0Aforeach ( $records as $record ) { =0A foreach(=
$record as $column=3D>$value ) {=0A echo "$column is $value\n";=0A =
}=0A}=0ASo the first foreach will iterate over each object of stdClass t=
hat=0Acorresponds to each record of mysql data fetched, then, for each of t=
hem,=0Atake the associative array key and the corresponding value...=0ACorr=
ect? :DDD =0AHowever, is this the right track to archive the goa=
l stated above, or, they=0Aare far better ways for doing so? Reg=
ards,=0AM=E1rcio -------------------------------------- ---------------=
------------------------------------------------------------ ---------------=
------------- =0ACorrect about my example, although I'm not sure if yo=
u 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 arr=
ay_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 an=
d 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 columns. Hope it helps.
--0-371926515-1255088471=:10597--

RE: Newbie: Array of objects iteration

am 09.10.2009 14:15:50 von talofo talofo

>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 >columns.
>
>Hope it helps.





Right now, I have something like this:

foreach ($objRecord as $record)
{=09
//we will have a new line.
$xls_file .=3D"\n";
=09
=09
foreach ($record as $column=3D>$value)=20
{
$xls_file .=3D $value."\t";
}

}

The only thing I need now, is to put on top, the column names, and I =
have
myself a xls file. YUpii!!=20

I just need the column names... I will take a look on those functions =
you
have mentioned.=20

Thanks a lot, :)
M=E1rcio



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

RE: Newbie: Array of objects iteration

am 09.10.2009 15:22:05 von talofo talofo

> Right now, I have something like this:
>=20
> foreach ($objRecord as $record)
> {
> //we will have a new line.
> $xls_file .=3D"\n";
>=20
>=20
> foreach ($record as $column=3D>$value)
> {
> $xls_file .=3D $value."\t";
> }
>=20
> }
>=20
> The only thing I need now, is to put on top, the column names, and I
> have myself a xls file. YUpii!!
>=20
> I just need the column names... I will take a look on those functions
> you have mentioned.
>=20
> Thanks a lot, :)
> M=E1rcio


Update:

"PDO::FETCH_OBJ
Returns an anonymous object with property names that correspond to the
column names returned in your result set."

I will give a shot on array_keys however, is there any method to access =
the
PROPERTIES NAMES of a given object?


Regards,
M.


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

RE: Newbie: Array of objects iteration

am 09.10.2009 19:19:23 von talofo talofo

Dear all, it's done.

Can I call your help for the remain issues please, I'm sure they are easy to
explain, and at some extend, common:

http://pastebin.com/m691d3e66

Instead of saving to the database, if I do a print_r or a var_dump, I get
the charsets quite ok.
However, they do not appear properly in the xls file.

I've tried to use utf8_encode but I was not sure where to apply it, and I
end up with the same strange chars on the .xls file.

If I write a generates the file, I get headers already send message.

I've tried to add Content-type: application/x-msdownload; *charset=UTF-8;*
but no success either...



Would a proper use of utf8_encode solve this? If so, where?



Thanks in advance,
M.



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

RE: Newbie: Array of objects iteration

am 09.10.2009 20:29:56 von talofo talofo

Done.=20

print utf8_decode($xls_file);


Regards,
M=E1rcio

> -----Original Message-----
> From: MEM [mailto:talofo@gmail.com]
> Sent: sexta-feira, 9 de Outubro de 2009 18:19
> To: 'MEM'; 'Fernando Castillo Aparicio'; 'Lester Caine'; 'php-
> general@lists.php.net'
> Subject: RE: [PHP] Newbie: Array of objects iteration
>=20
> Dear all, it's done.
>=20
> Can I call your help for the remain issues please, I'm sure they are
> easy to explain, and at some extend, common:
>=20
> http://pastebin.com/m691d3e66
>=20
> Instead of saving to the database, if I do a print_r or a var_dump, I
> get the charsets quite ok.
> However, they do not appear properly in the xls file.
>=20
> I've tried to use utf8_encode but I was not sure where to apply it, =
and
> I end up with the same strange chars on the .xls file.
>=20
> If I write a > that generates the file, I get headers already send message.
>=20
> I've tried to add Content-type: application/x-msdownload; =
*charset=3DUTF-
> 8;* but no success either...
>=20
>=20
>=20
> Would a proper use of utf8_encode solve this? If so, where?
>=20
>=20
>=20
> Thanks in advance,
> M.



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

Re: Newbie: Array of objects iteration

am 09.10.2009 23:23:31 von Tommy Pham

----- Original Message ----=0A> From: MEM =0A> To: MEM alofo@gmail.com>; Fernando Castillo Aparicio ; Lester =
Caine ; php-general@lists.php.net=0A> Sent: Fri, Octobe=
r 9, 2009 6:22:05 AM=0A> Subject: RE: [PHP] Newbie: Array of objects iterat=
ion=0A> =0A> > Right now, I have something like this:=0A> > =0A> > foreach =
($objRecord as $record)=0A> > {=0A> > //we will have a new line.=0A> > =
$xls_file .=3D"\n";=0A> > =0A> > =0A> > foreach ($record as $column=
=3D>$value)=0A> > {=0A> > $xls_file .=3D $value."\t";=0A> > =
}=0A> > =0A> > }=0A> > =0A> > The only thing I need now, is to put on top,=
the column names, and I=0A> > have myself a xls file. YUpii!!=0A> > =0A> >=
I just need the column names... I will take a look on those functions=0A> =
> you have mentioned.=0A> > =0A> > Thanks a lot, :)=0A> > M=E1rcio=0A> =0A>=
=0A> Update:=0A> =0A> "PDO::FETCH_OBJ=0A> Returns an anonymous object with=
property names that correspond to the=0A> column names returned in your re=
sult set."=0A> =0A> I will give a shot on array_keys however, is there any =
method to access the=0A> PROPERTIES NAMES of a given object?=0A> MEM,=
http://www.php.net/language.oop5.reflection =0ARe gards,=0ATommy=
=0A> =0A> Regards,=0A> M.=0A> =0A> =0A> --=0A> PHP General Mailing Li=
st (http://www.php.net/)=0A> To unsubscribe, visit: http://www.php.net/unsu=
b.php=0A

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

RE: Newbie: Array of objects iteration

am 10.10.2009 11:49:23 von talofo talofo

>=20
> MEM,
>=20
> http://www.php.net/language.oop5.reflection
>=20
>=20
> Regards,
> Tommy
>=20
>=20


And brand new world opens in from of my eyes... O.O.=20

I will search more info on this on the net... just for the records, as
properties names is concern, I couldn't find any better I suppose:
http://pt.php.net/manual/en/reflectionproperty.getname.php


I'm just wondering, we call it like this?
$property =3D new ReflectionProperty($myproperty);
$proptertyName =3D $property->getName();


Can we called statically like this?
$propertyName =3D ReflectionProperty::getName($myproperty); -> this =
would
be nice. :)

The documentation is a little bit lacking, and I'm a little bit newbie, =
is
this correct?

Note:
The array_keys as a possibility to retrieve ALL keys from a given array. =
It
would be nice to retrieve ALL properties names at once as well...
:)



Regards,
M=E1rcio


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

Re: Newbie: Array of objects iteration

am 11.10.2009 01:34:48 von Lars Torben Wilson

2009/10/10 MEM :
>>
>> MEM,
>>
>> http://www.php.net/language.oop5.reflection
>>
>>
>> Regards,
>> Tommy
>>
>>
>
>
> And brand new world opens in from of my eyes... O.O.
>
> I will search more info on this on the net... just for the records, as
> properties names is concern, I couldn't find any better I suppose:
> http://pt.php.net/manual/en/reflectionproperty.getname.php
>
>
> I'm just wondering, we call it like this?
> $property =3D new ReflectionProperty($myproperty);
> $proptertyName =3D $property->getName();
>
>
> Can we called statically like this?
> $propertyName =3D ReflectionProperty::getName($myproperty); =A0 =A0 =A0->=
this would
> be nice. :)
>
> The documentation is a little bit lacking, and I'm a little bit newbie, i=
s
> this correct?
>
> Note:
> The array_keys as a possibility to retrieve ALL keys from a given array. =
It
> would be nice to retrieve ALL properties names at once as well...
> :)
>
>
>
> Regards,
> M=E1rcio

Hi M=E1rcio,

You may also find the following useful:

http://php.net/get_class_vars
http://php.net/get_object_vars
http://php.net/reflectionclass.getproperties


Regards,

Torben

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

Re: Newbie: Array of objects iteration

am 12.10.2009 00:47:35 von Tommy Pham

----- Original Message ----=0A> From: MEM =0A> To: Tommy =
Pham ; php-general@lists.php.net=0A> Sent: Sat, October=
10, 2009 2:49:23 AM=0A> Subject: RE: [PHP] Newbie: Array of objects iterat=
ion=0A> =0A> > =0A> > MEM,=0A> > =0A> > http://www.php.net/language.oop5.re=
flection=0A> > =0A> > =0A> > Regards,=0A> > Tommy=0A> > =0A> > =0A> =0A> =
=0A> And brand new world opens in from of my eyes... O.O. =0A> =0A> I will =
search more info on this on the net... just for the records, as=0A> propert=
ies names is concern, I couldn't find any better I suppose:=0A> http://pt.p=
hp.net/manual/en/reflectionproperty.getname.php=0A> =0A> =0A> I'm just wond=
ering, we call it like this?=0A> $property =3D new ReflectionProperty($mypr=
operty);=0A> $proptertyName =3D $property->getName();=0A> =0A> =0A> Can we =
called statically like this?=0A> $propertyName =3D ReflectionProperty::getN=
ame($myproperty); -> this would=0A> be nice. :)=0A> =0A> The documenta=
tion is a little bit lacking, and I'm a little bit newbie, is=0A> this corr=
ect?=0A> =0A> Note:=0A> The array_keys as a possibility to retrieve ALL key=
s from a given array. It=0A> would be nice to retrieve ALL properties names=
at once as well...=0A> :)=0A> =0A> =0A> =0A> Regards,=0A> M=E1rcio=0A> =0A=
> =0A> --=0A> PHP General Mailing List (http://www.php.net/)=0A> To unsubsc=
ribe, visit: http://www.php.net/unsub.php M=E1rcio, Sorry, I don'=
t remember since it's been over 5 years I used reflection. Reflection has =
many uses, 1 of it is that it allow you reverse engineer any PHP app ;)=0A=
=0ARegards,=0ATommy=0A

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