PHP code example of sitnikovik / array-to-object-serializer

1. Go to this page and download the library: Download sitnikovik/array-to-object-serializer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sitnikovik / array-to-object-serializer example snippets


// Array to convert
$array = [
    "name" => "csgo",
    'players' => [
        [
            "name" => "bonnie",
            "health" => 100,
            "weapon" => [
                "name" => "m4a1",
                "ammo" => 30,
            ],
        ],
        [
            "name" => "klyde",
            "health" => 100,
            "weapon" => [
                "name" => "ak47",
                "ammo" => 30,
            ],
        ],
    ],
];

// Convert array to object with one step
$object = \Sitnikovik\ArrayToObjectSerializer\ArrayToObject::serialize($array, "needed_class_name_with_namespace");

use Sitnikovik\ArrayToObjectSerializer\ToObject;

class Game
{
    /**
     * @var string
     */
    public $name;

    /**
     * @var Player[]
     *
     * @ToObject(Sitnikovik\ArrayToObjectSerializer\Mock\Player)
     */
    public $players = [];
}