Oihana PHP

toAssociativeArray.php

Table of Contents

Functions

toAssociativeArray()  : array<string|int, mixed>
Recursively converts an object (or array) into a full associative array.

Functions

toAssociativeArray()

Recursively converts an object (or array) into a full associative array.

toAssociativeArray(array<string|int, mixed>|object $document) : array<string|int, mixed>

This function handles nested objects, ensuring the entire array or object tree is converted.

Note that only public properties of the object will be included in the resulting array.

Parameters
$document : array<string|int, mixed>|object

An array or object to convert to a deep associative array .

Tags
example

Convert an object :

// Define some classes for the example.
class Address
{
    public string $street = '123 PHP Avenue';
    public string $city = 'Codeville';
}

class User
{
    public int $id = 42;
    public string $name = 'John Doe';
    public Address $address;
    private string $sessionToken = 'a-very-secret-token'; // This will be ignored.

    public function __construct()
    {
        $this->address = new Address();
    }
}

$userObject = new User();

$userArray = toAssociativeArray($userObject);

print_r($userArray);
// Output:
// Array
// (
//     [id] => 42
//     [name] => John Doe
//     [address] => Array
//         (
//             [street] => 123 PHP Avenue
//             [city] => Codeville
//         )
// )
// Note that the private property 'sessionToken' is not present.

Convert an array with sub-objects:

$data = (object)
[
    'id' => 123,
    'name' => 'Project Alpha',
    'provider' => (object)
    [
       'name' => 'Alice',
       'role' => 'Chef de projet'
    ],
    'team' =>
     [
        (object) ['name' => 'Bob'     ] ,
        (object) ['name' => 'Charlie' ]
     ]
];

$arrayAssoc = toAssociativeArray($data);

print_r($arrayAssoc);
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
array<string|int, mixed>

The resulting associative array.


        
On this page

Search results