PHP

Convert Array of Standard Objects to Associative Arrays

Learn how to transform an array of `stdClass` objects, often returned by `json_decode` or database queries, into a more flexible array of associative arrays in PHP for easier manipulation.

<?php
function objectsToArray(array $objects): array
{
    $arrayOfArrays = [];
    foreach ($objects as $object) {
        if (is_object($object)) {
            $arrayOfArrays[] = (array) $object;
        }
    }
    return $arrayOfArrays;
}

// Example: Simulating data from json_decode(..., false) or a simple database fetch
$objectList = [
    (object)['id' => 1, 'name' => 'Item A', 'value' => 100],
    (object)['id' => 2, 'name' => 'Item B', 'value' => 200],
    (object)['id' => 3, 'name' => 'Item C', 'value' => 150],
];

$associativeArrayList = objectsToArray($objectList);

/*
Example Output:
[
    ["id" => 1, "name" => "Item A", "value" => 100],
    ["id" => 2, "name" => "Item B", "value" => 200],
    ["id" => 3, "name" => "Item C", "value" => 150]
]
*/

// Demonstrate with nested objects
$nestedObjectList = [
    (object)['id' => 101, 'data' => (object)['type' => 'digital', 'size' => 'small']],
    (object)['id' => 102, 'data' => (object)['type' => 'physical', 'weight' => 'heavy']],
];

// Note: This basic function only converts the top-level objects.
// For deep conversion, recursion would be needed.
$convertedNested = objectsToArray($nestedObjectList);

/*
Example Output for nested:
[
    ["id" => 101, "data" => (object)["type" => "digital", "size" => "small"]], // 'data' remains an object
    ["id" => 102, "data" => (object)["type" => "physical", "weight" => "heavy"]]
]

For full deep conversion, a recursive solution would be:
function deepObjectsToArray(array $objects): array {
    $arrayOfArrays = [];
    foreach ($objects as $object) {
        if (is_object($object)) {
            $temp = (array) $object;
            foreach ($temp as $key => $value) {
                if (is_object($value)) {
                    $temp[$key] = deepObjectsToArray([$value])[0]; // Recursively convert nested objects
                } elseif (is_array($value)) {
                    $temp[$key] = deepObjectsToArray($value); // Recursively convert nested arrays of objects
                }
            }
            $arrayOfArrays[] = $temp;
        }
    }
    return $arrayOfArrays;
}
$deepConvertedNested = deepObjectsToArray($nestedObjectList);
/*
Deep Converted Output:
[
    ["id" => 101, "data" => ["type" => "digital", "size" => "small"]],
    ["id" => 102, "data" => ["type" => "physical", "weight" => "heavy"]]
]
*/
How it works: This snippet provides a `objectsToArray` function that converts an array of `stdClass` objects into an array of associative arrays. This is particularly useful when working with data sourced from `json_decode` without the `true` flag, or from database query results, making data manipulation with array functions more straightforward. A commented-out `deepObjectsToArray` alternative is also provided for nested object structures.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs