Data handler for PHP arrays.
- PHP >= 8.1
Install package of the data handler via Composer...
composer require jnessier/data-handler
...or manually download the latest release from here.
use DataHandler\Data;
// Create new data handler.
$data = new Data([
// Array with key/value pairs
]);
// Get value by key.
$default = null; // Default value, when key doesn't exists
$value = $data->get('key', $default);
// Pull value by key and delete it afterward.
$default = null; // Default value, when key doesn't exists
$value = $data->pull('key', $default);
// Set value by key.
$overwrite = true; // Set FALSE to prevent overwrite existing value
$data = $data->set('key', 'value', $overwrite);
// Check whether value exists by key.
$valueExists = $data->has('key');
// Delete value by key.
$data->delete('key');
// Count number of values.
$numberOfValues = $data->count();
// Get all values as array.
$array = $data->getAll();
// Iterate trough values.
$data->each(function ($value, string $key) {
// Callback for each key/value pair
});
// Clear values.
$data = $data->clear();
// Replace values. Existing values with similar keys will be overwritten.
$recursive = true; // Set FALSE to prevent recursive merge
$data = $data->replace([
// Array with key/value pairs
], $recursive);
// Set array as values. Existing data will be overwritten.
$data = $data->setAll([
// Array with key/value pairs
]);
// Set referenced array as values. Existing data will be overwritten.
$values = [
// Array with key/value pairs
];
$data = $data->setAllReferenced($values);
- Only string as key supported
- Only array as key/value pairs supported
- No dot notation implementation
- No type check for values
- Jonathan Nessier (jnessier)
If you would like to see this library develop further, or if you want to support me or show me your appreciation, please donate any amount through PayPal. Thank you! 🍻
Licensed under MIT.
Made in Switzerland with 🧀 and ❤️