PHP code example of pflorek / elevator

1. Go to this page and download the library: Download pflorek/elevator 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/ */

    

pflorek / elevator example snippets


use PFlorek\Elevator\Elevator;
use PFlorek\Elevator\ElevatorFactory;
use function \PFlorek\Elevator\array_elevate;

$flattened = [
    'World.Asia.Afghanistan.0' => '...',
    'World.Africa' => true,
    'World.Antarctica' => -25.2,
    'World.Europe' => new \stdClass(),
    'World.North America' => [],
];

// object oriented
$factory = ElevatorFactory::getInstance();
$elevator = $factory->create();
$elevator->up($flattened);

// or functional
$elevated = array_elevate($flattened);

var_dump($elevated);

//returns ["World"] => array(5) {
//  ["Asia"] => array(1) {
//    ["Afghanistan"] => array(1) {
//      [0] => string(3) "..."
//    }
//  }
//  ["Africa"] => bool(true)
//  ["Antarctica"] => float(-25.2)
//  ["Europe"]=> object(stdClass)#298 (0) {}
//  ["North America"]=> []
//}

use PFlorek\Elevator\Elevator;
use PFlorek\Elevator\ElevatorFactory;
use function \PFlorek\Elevator\array_flatten;

$elevated = [
    'World' => [
        'Asia' => [
            'Afghanistan' => [
                '...'
            ]
        ],
        'Africa' => true,
        'Antarctica' => -25.2,
        'Europe' => new \stdClass(),
        'North America' => [],
    ]
];

// object oriented
$factory = ElevatorFactory::getInstance();
$elevator = $factory->create();
$elevator->down($flattened);

// or functional
$flattened = array_flatten($elevated);

var_dump($flattened);

//returns array(5) {
//  ["World.Asia.Afghanistan.0"] => string(3) "..."
//  ["World.Africa"] => bool(true)
//  ["World.Antarctica"] => float(-25.2)
//  ["World.Europe"] => object(stdClass) (0) { }
//  ["World.North America"] => array(0) { }
//}