Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


PHP Objects

Posted on 2013-01-10
Medium Priority
Last Modified: 2013-01-11
Greetings, I have a variable that is instantiated as a PHP Object


$stats has some key->values assigned to it

$stats->gender = "male";
$stats->age = 34;

if I want the gender I can:
<?PHP echo $stats->gender ?>

what if I want the fieldname, not fieldvalue...
ie.  I want "gender" to appear, not "male"...

Question by:Evan Cutler
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38766004
Use this

echo $key = key((array)$stats);

Author Comment

by:Evan Cutler
ID: 38766011
Thank you sivagnanum_c,

How do I differentiate one from another?
There's gender and age...
How do I pick one out?


Expert Comment

by:Vimal DM
ID: 38766015

just use the below code to print the field name

      echo key($stats);
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

by:Evan Cutler
ID: 38766027
Ok, the problem is in this scenario, I have two fields....
How do I tell one from the other with that snippet?


Assisted Solution

by:Vimal DM
Vimal DM earned 600 total points
ID: 38766034
This might the useful one for you,i think

foreach($stats as $key=>$value){
      echo $key.'::'.$value.'<br>';
LVL 12

Accepted Solution

sivagnanam chandrakanth earned 1400 total points
ID: 38766040
see example

$obj = (object)array("Test" => "bar","test2"=>"val2");

$array= get_object_vars($obj); 
foreach($array as $key=>$val){
echo $key;
//echo $key = key((array)$obj);

Open in new window

LVL 34

Expert Comment

ID: 38768295
greetings   arcee123  , , what you have said so far does not give me enough information about what you are trying to do AND what the Variables you have to start with ?

If  you know the property Name (sometimes referred to as the KEY) you just echo out that
echo "This is the Age ".$stats->age;

However you can NOT have a variable like  $var   without the  ->  ($var->property) and get any info about a single Property name, because there is NO property specified with $var

you say "I have two fields....  How do I tell one from the other with that snippet"
can you show us the PHP code (variables) for the two fields ? It may make a difference in how to solve this.

Author Closing Comment

by:Evan Cutler
ID: 38768313
Thanks guys...most appreciated.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question