I'm trying to print data from a stdClass object.

Array
(
    [1] => stdClass Object
        (
            [bid] => 1
            [weight] => 0
            [name] => Staff
            [image] => sites/default/files/image/stars.gif
            [href] => 
            [unhideable] => 0
            [fixedweight] => 0
            [doesnotcounttolimit] => 0
            [userweight] => 
            [tid] => 0
            [coalescedweight] => 0
            [class] => badge Staff
        )
)

Not sure how to print some of the labels and values.

----------------------------
Dinajpur- My District Portal,
Rangpur-Another Local Portal (Drupal Powered)
Banglajogot- Bangla Web Design Blog (Drupal Powered)
JoomlaBangla - Joomla! Bangla Support Site

Comments

jordojuice’s picture

print $object->name;
print $object->href;

...etc
Without knowing more context thats the basic way to print object data with the properties being string as they are here.

This object is in an array. So if you need to print a property from each object in an array you can do

foreach ($array as $object) {
  print $object->name;
}
kingandy’s picture

Not sure how to print some of the labels and values.

If you know the name of the object property, you can output it directly by combining array and object notation like so:

  print $object[1]->bid;

If you're after the labels, you can either use get_object_vars() or simply cast the object to an array - both of which end up with an array with the same structure as your original object.

get_object_vars:

$array = get_object_vars($object[1]);
print_r($array);

Casting:

$array = (array) $object[1];
print_r($array);

Both of which produce:

Array
(
    [bid] => 1
    [weight] => 0
    [name] => Staff
    [image] => sites/default/files/image/stars.gif
    [href] => 
    [unhideable] => 0
    [fixedweight] => 0
    [doesnotcounttolimit] => 0
    [userweight] => 
    [tid] => 0
    [coalescedweight] => 0
    [class] => badge Staff
)

You can then step through the resulting array and output key/value pairs, or extract the labels using array_keys(), or whatever it is you're wanting to do with it.

++Andy
Developing Drupal websites for Livelink New Media since 2008

yogi.ghorecha’s picture

BEst and useful

umanda’s picture

use this code

<?php
$foo // main array

print $foo[1]->bid;
?>

it should be work for you

Happy coding

Cheers