2
<?
stdClass Object
(
    [image_header] => Array
        (
            [0] => stdClass Object
                (
                    [img] => /headers/header.jpg
                )
        )
)
?>

Object name image_header is variable, so it can be any string. Can I access this string without knowing what it is?

@Jon his answer was satisfying for me.

For others who want to use variable objectnames this way: To acces this object with the variablename I had to use curly brackets:

$key = key(get_object_vars($_json));
$_json->{$key}[0]->img;
4
  • Is this the only property of the object? If not, which one do you want the name of? Commented Jan 23, 2013 at 16:02
  • It is the only one. I am trying to avoid a foreach loop ;-) Commented Jan 23, 2013 at 16:04
  • I added an answer, but I 'm afraid it also does the equivalent of foreach internally :) Commented Jan 23, 2013 at 16:06
  • Can live with that ;-) thanks Commented Jan 23, 2013 at 16:23

3 Answers 3

10

You can do it conveniently with get_object_vars:

$propertyName = key(get_object_vars($object));
Sign up to request clarification or add additional context in comments.

1 Comment

that only works for ONE item, not for multiple ones or nested ones
2

If you don't know what the name of the property is, you can use PHP's Reflection classes, or more simply use get_object_vars().

get_object_vars() is probably what you're looking for here - it "Returns an associative array of defined object accessible non-static properties for the specified object in scope. If a property has not been assigned a value, it will be returned with a NULL value." So, you get the property names and their values returned in an associative array.

Alternatively, you could use some of PHP's reflection magic, although it might be a bit overkill here, depending on your end goal. The reflection classes are very powerful, and may be worth using if you have more complex requirements for what you're trying to achieve. As an example:

// let's say $obj is the object you provided in your question
// Instantiate the reflection object
$reflector = new ReflectionClass($obj); 

// Get properties of $obj, returned as an array of ReflectionProperty objects
$properties = $reflector->getProperties();
foreach ( $properties as $property ) {
  echo $property->getName(); // In your example, this would echo 'image_header'
}

Comments

0

There are a couple of possibilities. If you're using json_decode() you can pass true in as the second parameter to parse the data as an associative array.

$data = json_decode($myJson, true);

print_r( $data['image_header'] );

You can also access an object property from a variable like this.

$myProperty = 'image_header';

print_r( $data->$myProperty );

If by "you don't know what it is" you mean you don't know the key at all you can use my first example and use array_values() to get the values by index.

$values = array_values($data);

// image_header
print_r( $values[0] );

1 Comment

Thanks, going for Jon's, it is the cleanest solution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.