1. Go to this page and download the library: Download sajadsdi/array-dot-notation 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/ */
sajadsdi / array-dot-notation example snippets
Sajadsdi\ArrayDotNotation\DotNotation;
// Create an instance of DotNotation with an array
$data = [
'user' => [
'profile' => [
'id' => 625,
'pic' => '625.png',
],
],
];
$dotNotation = new DotNotation($data);
// Get values using dot notation
$userId = $dotNotation->get('user.profile.id'); // $userId will be 625
$picPath = $dotNotation->get('user.profile.pic'); // $picPath will be '625.png'
// Check if a key exists
if ($dotNotation->has('user.profile.id')) {
// Key exists
} else {
// Key does not exist
}
// Set values using dot notation
$dotNotation->set(['user.profile.id' => 12345, 'user.profile.pic' => 'new_pic.png']);
// Get the updated value
$newUserId = $dotNotation->get('user.profile.id'); // $newUserId will be 12345
// Delete key(s) using dot notation
$dotNotation->delete('user.profile.id');
$user = $dotNotation->get();
/** The $user will be :
[
'user' => [
'profile' => [
'pic' => 'new_pic.png'
]
]
]
*/
//set again
$dotNotation->set(['user.profile.id' => 1234, 'user.profile.pic' => 'new_pic2.png']);
//multi keys deletion
$dotNotation->delete(['user.profile.id','user.profile.pic']);
$user = $dotNotation->get();
/** The $user will be :
[
'user' => [
'profile' => []
]
]
*/
$settings = [
'app' => [
'name' => 'My App',
'version' => '1.0',
],
'user' => [
'theme' => 'light',
],
];
// Initialize DotNotation with the $settings array
$dotNotation = new DotNotation($settings);
$result = $dotNotation->get(['app.name', 'app.version', 'user']);
// The result will be ['name' => 'My App', 'version' => '1.0', 'user' => ['theme' => 'light']]
$data = [
'user' => [
'profile' => [
'id' => 625,
'pic' => '625.png',
],
],
];
// Initialize DotNotation with the $data array
$dotNotation = new DotNotation($data);
$keys = ['profile_id' => 'user.profile.id', 'profile_photo' => 'user.profile.pic'];
$result = $dotNotation->get($keys);
// The result will be ['profile_id' => 625, 'profile_photo' => '625.png']
$array = [
'users' => [
['id' => 1, 'name' => 'John'],
['id' => 2, 'name' => 'Alice'],
['id' => 3, 'name' => 'Emma'],
['id' => 4, 'name' => 'Emily'],
['id' => 5, 'name' => 'Sofia'],
],
];
// Initialize DotNotation with the $array
$dotNotation = new DotNotation($array);
$keys = ['users.2.name', 'users.3.name', 'users.4.name'];
$result = $dotNotation->get($keys);
// The result will be ['name' => 'Emma', 'name_1' => 'Emily', 'name_2' => 'Sofia']
// The keys are ['users.2.name', 'users.3.name', 'users.4.name'], in fact [0 => 'users.2.name', 1 =>'users.3.name', 2 => 'users.4.name']
$data = [
'app' => [
'name' => 'My App',
'version' => '1.0',
],
'user' => [
'theme' => 'light',
],
];
// Initialize DotNotation with your data array
$dotNotation = new DotNotation($data);
// Get the value with a single default value if the key doesn't exist
$username = $dotNotation->get('user.profile.name', 'Guest');
echo "Username: $username"; // Output: Username: Guest
$data = [
'user' => [
'profile' => [
'pic' => '625.png',
],
],
];
// Initialize DotNotation with your data array
$dotNotation = new DotNotation($data);
// Get the value with a single default value if the key exists
$profilePic = $dotNotation->get('user.profile.pic', 'default_pic.png');
echo "Profile Picture: $profilePic"; // Output: Profile Picture: 625.png (value from the data)
$data = [
'user' => [
'profile' => [
'name' => 'John Doe',
],
],
];
// Initialize DotNotation with your data array
$dotNotation = new DotNotation($data);
// Get multiple values with a single default value for each key if the key doesn't exist
$profile = $dotNotation->get(['user.profile.name', 'user.profile.bio'], ['Guest', 'No bio available']);
// Output: ['name' => "John Doe", 'Bio' => "No bio available"]
$data = [
'user' => [
'profile' => [
'pic' => '625.png',
],
],
];
// Initialize DotNotation with your data array
$dotNotation = new DotNotation($data);
// Get multiple values with one default value for each key if the key doesn't exist
$profile = $dotNotation->get(
['user.profile.name', 'user.profile.bio'],'No available'
);
// Output: ['name' => "No available", 'Bio' => "No available"]
$data = [
'user' => [
'profile' => [
'name' => 'John Doe',
],
],
];
// Initialize DotNotation with your data array
$dotNotation = new DotNotation($data);
// Check if multiple keys exist using an array of keys
$keysToCheck = ['user.profile.name', 'user.profile.bio'];
if ($dotNotation->has($keysToCheck)) {
echo "All keys exist in the data.";
} else {
echo "At least one key does not exist in the data.";
}
$data = [
'user' => [
'profile' => [
'name' => 'John Doe',
],
],
];
// Initialize DotNotation with your data array
$dotNotation = new DotNotation($data);
// Check if at least one of the keys exists using an array of keys
$keysToCheck = ['user.profile.name', 'user.profile.bio'];
if ($dotNotation->hasOne($keysToCheck)) {
echo "At least one key exists in the data.";
} else {
echo "None of the keys exist in the data.";
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.