Skip to content
Advertisement

How can I use key value of foreach as property value of an object?

I want to print all the properties from an object.

Is there a way to use the key value as a property value of a object? instead of using get_object_vars.

Error

Undefined property: stdClass::$key

Update Example

JavaScript

Example of $arrayOfValues

JavaScript

Example of $value

JavaScript

Advertisement

Answer

I think that in your case it was a simple typo:

JavaScript

As the other mentioned you can use directly the array. No need to convert it to object:

JavaScript

If you want to display all the keys of that array you can simply use something like:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement