PHPackages                             jbzoo/data - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jbzoo/data

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jbzoo/data
==========

An extended version of the ArrayObject object for working with system settings or just for working with data arrays

7.2.0(7mo ago)891.6M—0.9%5[1 PRs](https://github.com/JBZoo/Data/pulls)20MITPHPPHP ^8.2CI passing

Since Aug 2Pushed 7mo ago6 watchersCompare

[ Source](https://github.com/JBZoo/Data)[ Packagist](https://packagist.org/packages/jbzoo/data)[ RSS](/packages/jbzoo-data/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (44)Used By (20)

JBZoo / Data
============

[](#jbzoo--data)

[![CI](https://github.com/JBZoo/Data/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/JBZoo/Data/actions/workflows/main.yml?query=branch%3Amaster) [![Coverage Status](https://camo.githubusercontent.com/b97a145da4cd1a7886493e1f14c155a2cd094f664d416f976cb7270b8ed3b4f6/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f4a425a6f6f2f446174612f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/JBZoo/Data?branch=master) [![Psalm Coverage](https://camo.githubusercontent.com/18d11fc74967a10eafed32dc1e893394f3c069fe06b7f81e0205ad6860ea794d/68747470733a2f2f73686570686572642e6465762f6769746875622f4a425a6f6f2f446174612f636f7665726167652e737667)](https://shepherd.dev/github/JBZoo/Data) [![Psalm Level](https://camo.githubusercontent.com/4e551097ceca1aa65df110cd0866f76868c153be810eb0b13ed4623d5fe46546/68747470733a2f2f73686570686572642e6465762f6769746875622f4a425a6f6f2f446174612f6c6576656c2e737667)](https://shepherd.dev/github/JBZoo/Data) [![CodeFactor](https://camo.githubusercontent.com/deacc1a2915359dfd9db3928e7210ed776655cc3dabaab6520c2b1379a1f4e35/68747470733a2f2f7777772e636f6465666163746f722e696f2f7265706f7369746f72792f6769746875622f6a627a6f6f2f646174612f6261646765)](https://www.codefactor.io/repository/github/jbzoo/data/issues)[![Stable Version](https://camo.githubusercontent.com/7535a2d6a37fe5fb006abe268506631ef4980be087e0918aa01ec5efbd5db1d0/68747470733a2f2f706f7365722e707567782e6f72672f6a627a6f6f2f646174612f76657273696f6e)](https://packagist.org/packages/jbzoo/data/) [![Total Downloads](https://camo.githubusercontent.com/844b5fad4b177a7acad34f4c1b82b7381bd8e41e318107b5081564693fc78817/68747470733a2f2f706f7365722e707567782e6f72672f6a627a6f6f2f646174612f646f776e6c6f616473)](https://packagist.org/packages/jbzoo/data/stats) [![Dependents](https://camo.githubusercontent.com/ebfcd9e0029a40d3c05d6d89c73997af3c2aac7d489788492d6ab2f7ff50b738/68747470733a2f2f706f7365722e707567782e6f72672f6a627a6f6f2f646174612f646570656e64656e7473)](https://packagist.org/packages/jbzoo/data/dependents?order_by=downloads) [![GitHub License](https://camo.githubusercontent.com/93ed195754204ef1fb611351c30dcda8338849db5247dc8ac397bf6b7382c1a8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6a627a6f6f2f64617461)](https://github.com/JBZoo/Data/blob/master/LICENSE)

An extended version of the [ArrayObject](http://php.net/manual/en/class.arrayobject.php) object for working with system settings or just for working with data arrays.

It provides a short syntax for daily routine, eliminates common mistakes. Allows you to work with various line and file formats - JSON, Yml, Ini, PHP arrays and simple objects.

- [Installation](#installation)
- [Usage](#usage)
    - [Comparison with pure PHP](#comparison-with-pure-php)
    - [Know your data](#know-your-data)
        - [Methods](#methods)
        - [Filter values (required JBZoo/Utils)](#filter-values-required-jbzooutils)
        - [Utility methods](#utility-methods)
        - [Export to pretty-print format](#export-to-pretty-print-format)
- [Summary benchmark info (execution time) PHP v8.2+](#summary-benchmark-info-execution-time-php-v82)
- [Unit tests and check code style](#unit-tests-and-check-code-style)
- [License](#license)
- [See Also](#see-also)

Installation
------------

[](#installation)

```
composer require jbzoo/data
```

Usage
-----

[](#usage)

### Comparison with pure PHP

[](#comparison-with-pure-php)

---

ActionJBZoo/DataPure PHP wayCreate`$d = data($someData)``$ar = [/* ... */];`Supported formatsArray, Object, ArrayObject, JSON, INI, YmlArrayLoad form file\*.php, \*.ini, \*.yml, \*.json, serialized-Get value or default`$d->get('key', 42)``$ar['key'] ?? 42`Get undefined #1`$d->get('undefined')` (no any notice)`$ar['undefined'] ?? null`Get undefined #2`$d->find('undefined')``$ar['und'] ??  null`Get undefined #3`$d->undefined === null` (no any notice)-Get undefined #4`$d['undefined'] === null` (no any notice)-Get undefined #5`$d['undef']['undef'] === null` (no any notice)-Comparing #1`$d->get('key') === $someVar``$ar['key'] === $someVar`Comparing #2`$d->is('key', $someVar)`-Comparing #3`$d->is('key', $someVar, true)` (strict)-Like array`$d['key']``$ar['key']`Like object #1`$d->key`-Like object #2`$d->get('key')`-Like object #3`$d->find('key')`-Like object #4`$d->offsetGet('key')`-Isset #1`isset($d['key'])``isset($ar['key'])`Isset #2`isset($d->key)``array_key_exists('key', $ar)`Isset #3`$d->has('key')`-Nested key #1`$d->find('inner.inner.prop', $default)``$ar['inner']['inner']['prop']` (error?)Nested key #2`$d->inner['inner']['prop']`-Nested key #3`$d['inner']['inner']['prop']`-Export to Serialized`echo data([/* ... */])``echo serialize([/* ... */])`Export to JSON`echo (json([/* ... */]))` (readable)`echo json_encode([/* ... */])`Export to Yml`echo yml([/* ... */])` (readable)-Export to Ini`echo ini([/* ... */])` (readable)-Export to PHP Code`echo phpArray([/* ... */])` (readable)-JSON**+**-Filters**+**-Search**+**-Flatten Recursive**+**-Set Value`$d['value'] = 42`$ar\['value'\] = 42Set Nested Value`$d->set('q.w.e.r.t.y') = 42`$ar\['q'\]\['w'\]\['e'\]\['r'\]\['t'\]\['y'\] = 42Set Nested Value (if it's undefined)`$d->set('q.w.e.r.t.y') = 42`PHP Notice errors...### Know your data

[](#know-your-data)

```
$json = json('{ "some": "thing", "number": 42 }');
dump($json->getSchema();
// [
//     "some" => "string",
//     "number" => "int"
// ]
```

#### Methods

[](#methods)

```
use function JBZoo\Data\data;
use function JBZoo\Data\ini;
use function JBZoo\Data\json;
use function JBZoo\Data\phpArray;
use function JBZoo\Data\yml;

$config = data([/* Assoc Array */]);       // Any PHP-array or simple object, serialized data
$config = ini('./configs/some.ini');       // Load configs from ini file (or string, or simple array)
$config = yml('./configs/some.yml');       // Yml (or string, or simple array). Parsed with Symfony/Yaml Component.
$config = json('./configs/some.json');     // JSON File (or string, or simple array)
$config = phpArray('./configs/some.php');  // PHP-file that must return array

// Read
$config->get('key', 42);                   // Returns value if it exists oR returns default value
$config['key'];                            // As regular array
$config->key;                              // As regular object

// Read nested values without PHP errors
$config->find('deep.config.key', 42);      // Gets `$config['very']['deep']['config']['key']` OR returns default value

// Write
$config->set('key', 42);
$config['key'] = 42;
$config->key = 42;

// Isset
$config->has('key');
isset($config['key']);
isset($config->key);

// Unset
$config->remove('key');
unset($config['key']);
unset($config->key);
```

#### Filter values (required JBZoo/Utils)

[](#filter-values-required-jbzooutils)

List of filters - [JBZoo/Utils/Filter](https://github.com/JBZoo/Utils/blob/master/src/Filter.php)

- `bool` - Converts many english words that equate to true or false to boolean.
- `int` - Smart converting to integer
- `float` - Smart converting to float
- `digits` - Leaves only "0-9"
- `alpha` - Leaves only "a-zA-Z"
- `alphanum` - Combination of `digits` and `alpha`
- `base64` - Returns only chars which are compatible with base64
- `path` - Clean FS path
- `trim` - Extend trim
- `arr` - Converting to array
- `cmd` - Cleanup system command (CLI)
- `email` - Returns cleaned up email or null
- `strip` - Strip tags
- `alias` - Sluggify
- `low` - String to lower (uses mbstring or symfony polyfill)
- `up` - String to upper (uses mbstring or symfony polyfill)
- `clean` - Returns safe string
- `html` - HTML escaping
- `xml` - XML escaping
- `esc` - Escape chars for UTF-8
- `function($value) { return $value; }` - Your custom callback function

```
$config->get('key', 42, 'int');         // Smart converting to integer
$config->find('key', 42, 'float');      // To float
$config->find('no', 'yes', 'bool');     // Smart converting popular word to boolean value
$config->get('key', 42, 'strip, trim'); // Chain of filters

// Your custom handler
$config->get('key', 42, function($value) {
    return (float)str_replace(',', '.', $value);
});
```

#### Utility methods

[](#utility-methods)

```
$config->search($needle);       // Find a value also in nested arrays/objects
$config->flattenRecursive();    // Return flattened array copy. Keys are NOT preserved.
```

#### Export to pretty-print format

[](#export-to-pretty-print-format)

```
echo $config;

$result = '' . $config;
$result = (string)$config;
$result = $config->__toString();
```

Example of serializing the `JSON` object

```
{
    "empty": "",
    "zero": "0",
    "string": " ",
    "tag": "Google.com",
    "array1": {
        "0": "1",
        "1": "2"
    },
    "section": {
        "array2": {
            "0": "1",
            "12": "2",
            "3": "3"
        }
    },
    "section.nested": {
        "array3": {
            "00": "0",
            "01": "1"
        }
    }
}
```

Example of serializing the `PHPArray` object

```
