Avatar ·

How to iterate over a PHP object with dynamic keys (property names)

Hi everyone. I'm new to PHP and encountered a problem when working with JSON data. After decoding JSON using `json_decode()`, I get a stdClass object whose properties (keys) are unknown in advance. For example, I receive product data from an external API, and the field names can change (price, quantity, description — but the exact set of fields is unknown). I need to iterate over all properties of this object and display them on the screen. Here's an example code that doesn't work for me: ```php $json = '{"item1": {"name": "Product A", "price": 150}, "item2": {"name": "Product B", "price": 250}}'; $data = json_decode($json); // Trying to iterate as an array — error foreach ($data as $key => $value) { echo $key . ': ' . $value . '
'; } ``` This code gives an error: "Invalid argument supp