PHP code example of nicmart / arrayze

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

    

nicmart / arrayze example snippets


class Person
{
    private $firstName;
    private $lastName;
    private $birthYear;

    public function __construct($firstName, $surname, $birthYear) { ... }

    public function getFirstName() { return $this->firstName; }
    public function getLastName() { return $this->lastName; }
    public function getBirthYear() { return $this->birthYear; }
}

use NicMart\Arrayze\MapsCollection;

$maps = (new MapsCollection)->registerMaps([
    "first name" =>   function(Person $p) { return $p->getFirstName(); },
    "last name" =>    function(Person $p) { return $p->getFirstName(); },
    "full name" =>    function($_, $x) { return "{$x['first name']} {$x['last name']}"; },
    "age" =>          function(Person $p) { return date("Y") - $p->getBirthYear(); },
    "name and age" => function($_, $x) { return "{$x['full name']}, {$x['age']}" }
]);

use NicMart\Arrayze\ArrayAdapter;

$nic = new Person("Nicolò", "Martini", 1983);

$arrayzedNic = new ArrayAdapter($nic, $maps);

echo $arrayzedNic["full name"];    // Prints "Nicolò Martini"
echo $arrayzedNic["age"];          // Prints 31
echo $arrayzedNic["name and age"]; // Prints "Nicolò Martini, 31"

foreach ($arrayzedNic as $key => $value)
    echo "$key: $value\n";
    
// Prints
// first name: Nicolò
// last name: Martini
// full name: Nicolò Martini
// age: 31
// name and age: Nicolò Martini, 31