Link

Introduction

I was looking for a simple way to manipulate complex PHP data structures. A quick research found a few solutions, which all fell short in some way.

The Dataset object takes an associative array, whose members are either arrays or objects. It provides SQL-like calls to search and manipulate data, and offers JSON files as a storage medium. Other storage solutions can be added easily with subclassing.

This Dataset implementation offers

  • Easy handling of JSON data
  • multiple search possibilities, including regular expressions
  • hiding the differences between PHP arrays and objects
  • multiple storage backends

Each search operation returns a new Dataset containing the results of the search only. The data of these child datasets is always stored by reference, which saves memory and makes updates easy. Of course, accidential updates may go unnoticed, so this concept needs to be used with caution.

Almost all Dataset methods return a Dataset instance, either a new dataset, or the dataset itself, so calls can be chained easily. Datasets containing intermediate results may be saved for later use.

Here is an example. Assuming a JSON database of persons with the following example record:

{
    "name" : "John Smith",
    "age": 44,
    "address" : {
        "street" : "1234 Wonder St",
        "zip" : 94016,
        "city" : "San Jose",
        "state" : "CA"
    }
}

You can easily iterate over, say, retired persons with an age of 65 or more:

foreach ($file->where('age', '>=', 65)->fetch() as $record) ...

Or if you only need NY retirees:

foreach ($file->where('age', '>=', 65)->where('address.state' '=', 'NY')->fetch() as $record) ...

Some terminology if you are going to read the docs:

The master dataset is the top-level dataset containing the full data, like a file or a dataset that you created.

Child datasets are the results of searches, or limits to the dataset.

Parent datasets are the datasets that a search result originates from.