PHP code example of devuri / dot-access

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

    

devuri / dot-access example snippets



use Urisoft\DotAccess;


$data = [
    'user' => [
        'name' => 'John Doe',
        'email' => '[email protected]',
        'address' => [
            'city' => 'New York',
            'country' => 'USA',
        ],
    ],
];

$dotdata = new DotAccess($data);

$name = $dotdata->get('user.name');
$email = $dotdata->get('user.email');
$city = $dotdata->get('user.address.city');

$dotdata->set('user.age', 30);

$emailExists = $dotdata->has('user.email');

$dotdata->remove('user.address.country');

$data = [
    'user' => [
        'name' => 'John Doe',
        'email' => '[email protected]',
        'address' => [
            'city' => 'New York',
            'country' => 'USA',
        ],
    ],
];

$dotdata = new DotAccess($data);

$name = $dotdata->get('user.name'); // Output: "John Doe"
$dotdata->set('user.age', 30);
$emailExists = $dotdata->has('user.email'); // Output: true
$dotdata->remove('user.address.country');

echo "Name: $name\n";
echo "Age: " . $dotdata->get('user.age') . "\n";
echo "Email exists: " . ($emailExists ? 'Yes' : 'No') . "\n";

$data = [
    'user' => [
        'name' => 'John Doe',
        'email' => '[email protected]',
        'address' => [
            'city' => 'New York',
            'country' => 'USA',
        ],
    ],
];

// Using the wrapper function
$name = DataKey:get($data, 'user.name');
$email = DataKey:get($data, 'user.email');
$city = DataKey:get($data, 'user.address.city');
$zipCode = DataKey:get($data, 'user.address.zip_code', 'N/A'); // Provide a default value if the key doesn't exist

echo "Name: $name\n";
echo "Email: $email\n";
echo "City: $city\n";
echo "Zip Code: $zipCode\n";