PHP code example of diephp / data-object

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

    

diephp / data-object example snippets


use DiePHP\DataObject;

$data = new DataObject([
    'name' => 'John Doe',
    'email' => '[email protected]'
]);

$data = DataObject::of([
    'name' => 'John Doe',
    'email' => '[email protected]'
]);

echo $data->name; // John Doe
echo $data['email']; // [email protected]

if ($data->has('name')) {
    // Key exists
}

$data->set('name', 'Jane Doe');

$email = $data->get('email', '[email protected]');

$data->merge([
    'address' => '123 Main St',
    'phone' => '123-456-7890'
]);

$data->filter(function ($value, $key) {
    return !empty($value);
});

$mappedData = $data->map('*', function ($value) {
    return strtoupper($value);
});

$array = $data->toArray();

$json = json_encode($data);

$hash = $data->hash();

$clone = $data->clone();

use DiePHP\DataObject;

$data = new DataObject([
    'user' => [
        'name' => 'John Doe',
        'email' => '[email protected]'
    ]
]);

echo $data->get('user.name'); // John Doe

$data->set('user.address', '123 Main St');
echo $data->get('user.address'); // 123 Main St

$data->merge(['user.phone' => '123-456-7890']);
echo $data->get('user.phone'); // 123-456-7890

$data->filter(function ($value, $key) {
    return !empty($value);
});

echo $data->hash(); // MD5 hash of the data

echo json_encode($data); // JSON representation