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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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

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
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

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
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

cycledudeAuthor Commented:
thanks, i tried this and I get

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

thanks
cycledudeAuthor Commented:
cheers
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.