php get variable names from an array/object

Hi

I have some output from a mysql  database query and when I do a print_r I get something like this:

Array
(
    [0] => stdClass Object
        (
            [order_date] => 2015-03-17
            [name] => some name
            [branch] => Some branch
            [total] => 11.40
        )

    [1] => stdClass Object
        (
            [order_date] => 2015-03-17
            [name] => Some other name
            [branch] => Some other branch
            [total] => 31.20
        )

    [2] => stdClass Object
        (
            [order_date] => 2015-03-12
            [name] => Last one
            [branch] => Last branch
            [total] => 37.20
        )

  ...

}

Open in new window


what i want to do is get the names of the fields (order_date,name,branch,total)
how can i get them through code?

just so that I can use the names as headers...

cheers
cycledudeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
You have an array of objects, so you'll need to loop through the array to access each object, and then get the properties of the object. Assume your array is called $data:

foreach ($data as $record):
    echo $record->order_date;
    echo $record->name;
    echo $record->branch;
    echo $record->total;
endforeach;

Open in new window

0
cycledudeAuthor Commented:
hi, sorry if i confused anyone...

i know how to get the data, it's the field names I want to access, in order to auto generate a table with them as headings...

like this.... kind of

| order_date | branch | name | total
--------------------------------------------------------
| 01-01-2015 | some | some | 100
0
Chris StanyonWebDevCommented:
Sorry - just realised I mis-read what you needed. To get the actual property names you can use the get_object_vars():

Again, assuming your array is called $data:

$fieldNames = get_object_vars($data[0]);

Open in new window


You will then have an array of fields, and you can loop them like so:

foreach ($fieldNames as $field => $value):
    echo $field;
endforeach;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

cycledudeAuthor Commented:
thanks, i tried this and I get

get_object_vars() expects parameter 1 to be object, array given
0
cycledudeAuthor Commented:
i take it back, it works a treat ;o)

thanks
0
cycledudeAuthor Commented:
cheers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.