2017 © Pedro Peláez
 

library array-property

Access arrays in an intuitive and object-oriented way. Keys and values are treated as properties.

image

mmamedov/array-property

Access arrays in an intuitive and object-oriented way. Keys and values are treated as properties.

  • Wednesday, May 18, 2016
  • by mmamedov
  • Repository
  • 1 Watchers
  • 1 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Build Status Latest Stable Version License, (*1)

ArrayProperty

Syntax sugar for working with arrays in PHP. Read and write to arrays in an object-oriented style. Keys are treated as properties., (*2)

ArrayProperty can be used on any PHP array variable., (*3)

Install using composer (recommended):, (*4)

composer require mmamedov/array-property

Or manually add to your composer.json file:, (*5)

"require": {
    "mmamedov/array-property": "^1.1"
}

Usage

Consider the following sample PHP array:, (*6)

$sample =
    array(
        'app' => array(
            'log_dir' => '/log/path',
            'debug' => false,
            'log' => true,
            'version' => '2.3',
            'deep' => array(
                'inner' => 'some value',
                'level' => '2'
            )
        ),
        'my node' => 'some value'
    );

This is how easy to work with it using ArrayProperty:, (*7)

$a = new ArrayProperty($sample);
echo $a->app->log_dir;      //outputs /log/path
echo $a->app->deep->inner;  //outputs 'some value'

//convert to array
$deep = $a->app->deep->toArray();
print_r($deep);             //outputs deep as array;

//check if value exists:
$a->app->exist('log_dir')   //returns true

Load new array and acces its values inline:, (*8)

$prop = new ArrayProperty(array());
$new = array(1 => 'apple', 2 => 'orange', 3 => 'olive', 4 => 'grapes', 'multi'=>array('key'=>'value'));
echo $prop->loadArray($new)->{1}; //outputs "apple"
echo $prop->multi->key;           //outputs "value"

Now let's add new element to the $prop ArrayProperty object we created above:, (*9)

$prop->myNewNode = "myValue";   //you can assign arrays as well.
echo $prop->myNewNode;          //outputs 'myValue';
//overwrite existing elements
$prop->{1} = "banana"; 
echo $prop->{1};                //now outputs "banana" instead of "apple"

Please note that multidimensional write is not supported, like in $prop->firstIndex->second = "value";, (*10)

You can switch modes between OBJECT_MODE and MIXED_MODE(default). Mode is set as second parameter in the constructor, or by calling setMode() method. In MIXED_MODE array values are returned in their original type, whereas in OBJECT_MODE returned values are always ArrayProperty object:, (*11)

$prop->setMode(ArrayProperty::OBJECT_MODE);
var_dump($prop->myNewNode);     //returns ArrayProperty object

$prop->setMode(ArrayProperty::MIXED_MODE);
var_dump($prop->myNewNode);     //returns "myValue" which was set before

For more examples see code inside ArrayProperty examples directory., (*12)

Enjoy!, (*13)

The Versions

18/05 2016

dev-master

9999999-dev

Access arrays in an intuitive and object-oriented way. Keys and values are treated as properties.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

array arrays array access array property php arrays

15/05 2016

v1.1

1.1.0.0

Access arrays in an intuitive and object-oriented way. Keys and values are treated as properties.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

array arrays array access array property php arrays

15/05 2016

v1.0.1

1.0.1.0

Access arrays in an intuitive and object-oriented way. Keys and values are treated as properties.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

array arrays array access array property php arrays

14/05 2016

v1.0

1.0.0.0

Access arrays in an intuitive and object-oriented way. Keys and values are treated as properties.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

array arrays array access array property php arrays