Solved

PHP Objects

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

$stats

$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"...

Thanks.
0
Comment
Question by:Evan Cutler
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38766004
Use this

echo $key = key((array)$stats);
0
 
LVL 9

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?

Thanks
0
 
LVL 7

Expert Comment

by:Vimal DM
ID: 38766015
Hi,

just use the below code to print the field name

<?PHP
      echo key($stats);
 ?>
0
 
LVL 9

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?

Thanks.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Assisted Solution

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

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

Accepted Solution

by:
sivagnanam chandrakanth earned 350 total points
ID: 38766040
see example

<?php
$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

0
 
LVL 33

Expert Comment

by:Slick812
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.
0
 
LVL 9

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now