2017 © Pedro Peláez
 

library multi-array

Provides a data structure to ease access to multidimensional arrays in PHP

image

tebru/multi-array

Provides a data structure to ease access to multidimensional arrays in PHP

  • Wednesday, August 5, 2015
  • by natebrunette
  • Repository
  • 2 Watchers
  • 3 Stars
  • 1,671 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 3 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Build Status Coverage Status Scrutinizer Code Quality, (*1)

MultiArray

This project aims to provide easier access to multidimensional arrays. Json responses were in mind during the creation. The goal is to make it easier to check if keys are set multiple levels deep, and retrieve the value., (*2)

Installation

composer require tebru/multi-array:~0.1

Usage

Create the object by either instantiating it or using the factory. i Pass in a json object or array. An InvalidArgumentException will be thrown if it can't decode the json, or the variable isn't an array., (*3)

$array = [
    'key' => 'value',
    'key2' => ['nested-key' => 'value2'],
];
$multiArray = new MultiArray($array);
$multiArray = $multiArrayFactory->make($array);

$json = json_encode($array);
$multiArray = new MultiArray($json);
$multiArray = $multiArrayFactory->make($json);

From here you can check if a key exists, (*4)

$multiArray->exists('key2.nested-key'); // returns true
$multiArray->exists('key3'); // returns false

Get the value of a key, (*5)

$multiArray->get('key2'); // returns ['nested-key' => 'value2']
$multiArray->get('key2.nested-key'); // returns 'value2'
$multiArray->get('key3'); // throws OutOfBoundsException

Set the value of a key, (*6)

$multiArray->set('key1', 'value');
$multiArray->set('key2.nested-key', 'value');
$multiArray->set('key2.newKey', 'value');
$multiArray->set('key1.newKey', 'value'); // throws InvalidArgumentException

Remove the value of a key, (*7)

$multiArray->remove('key1');
$multiArray->remove('key2.unknown'); // throws OutOfBoundsException

We default to delimiting keys by a period ., but that can be changed during instantiation, (*8)

$multiArray = new MultiArray($array, ':');
$multiArray = $multiArrayFactory->make($array, '--');

You can also access the object like a normal array, (*9)

isset($multiArray['key2.nested-key']);
$multiArray['key2.nested-key'];
$multiArray['key2.nested-key'] = 'value';
unset($multiArray['key2.nested-key']);

The Versions

05/08 2015

dev-master

9999999-dev

Provides a data structure to ease access to multidimensional arrays in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Nate Brunette

17/07 2015

v0.2.1

0.2.1.0

Provides a data structure to ease access to multidimensional arrays in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Nate Brunette

22/11 2014

v0.2.0

0.2.0.0

Provides a data structure to ease access to multidimensional arrays in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Nate Brunette

22/11 2014

v0.1.0

0.1.0.0

Provides a data structure to ease access to multidimensional arrays in PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Nate Brunette