PHPackages                             aklump/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. aklump/data

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

aklump/data
===========

Common interface for interacting with data in objects and arrays.

0.0.28(8y ago)0610BSD-3-ClausePHPPHP &gt;=5.3.0

Since Oct 3Pushed 2y ago1 watchersCompare

[ Source](https://github.com/aklump/data)[ Packagist](https://packagist.org/packages/aklump/data)[ Docs](http://github.com/aklump/data)[ RSS](/packages/aklump-data/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (28)Used By (0)

Data
====

[](#data)

---

\*\*\*\*\*\*\*\*\*\*\*\*\* ABANDONED \*\*\*\*\*\*\*\*\*\*\*\*\*

Suggested replacement:

-

```
// Setter
$setter = (new \Dflydev\DotAccessData\Data($multi_array));
$setter->set('do.re.mi', $value);
$multi_array = $setter->export();

// Getter
print (new \Dflydev\DotAccessData\Data($multi_array))
  ->get('do.re.mi', 'default');
```

---

Summary
-------

[](#summary)

Provides a common means for getting data from objects or arrays with default option such as Lodash's get method. Other methods for working with array/object data will be added in time.

Rationale
---------

[](#rationale)

Given a multidimensional array, in vanilla PHP you will do this:

```
print isset($multi_array['do']['re']) ? $multi_array['do']['re'] : 'default';

```

Using this class you would do this:

```
$data = new Data;
print $data->get($multi_array, 'do.re', 'default');

```

Not too impressive... but wait... imagine when you have complex objects, like say a Drupal 7 field value translated into spanish.

```
print isset($node->field_description['es']['1']['value']) ? $node->field_description['es']['1']['value'] : '';

// vs...

print $data->get($node, 'field_description.1.value', '');

```

Or when you need to work in Drupal 8 for a few days.

```
print isset($node->field_description) ? $node->get('field_description')->get(1)->value : '';

vs.

print $data->get($node, 'field_description.1.value', '');

```

This is where a consistent interface approach starts to make sense. By the way, there is a Drupal module that uses a different implementation of this class which can be found [here](https://www.drupal.org/project/data_api).

Default value
-------------

[](#default-value)

Every call to ::get can have a default value, which removes the need for if/thens or issets.

```
$country = $data->get($record, 'home.address.country', 'U.S.');

```

Callback
--------

[](#callback)

You can pass a callback to process the value such as loading a record by id.

```
$url = $data->get($record, 'picture.id', 'http://mysite.com/default.jpg', function($id) {
    return load_url_by_record_id($id);
});

```

Details
-------

[](#details)

```
