PHP Classes

Nested PHP Accessor: Get and set values of nested arrays or objects

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 58 All time: 10,551 This week: 57Up
Version License PHP version Categories
nested-accessor-php 1.0MIT/X Consortium ...7.4Algorithms, Libraries, PHP 7, PSR
Description 

Author

This package can get and set values of nested arrays or objects.

It can take an object or an array as a parameter to access the values of its elements.

The package provides accessor functions that take a string parameter that defines the path of the array or object element you want to access.

The path string uses the dot notation that separates the names of the elements and parent elements.

Currently, the package provides accessor functions to:

- Get the value of an element

- Set the value of an element

- Check if there is an element in a position with the given path

- Check if an element is set to a non-null value

- Append new values to the end of a given element

- Delete elements with the given path

Innovation Award
PHP Programming Innovation award nominee
April 2023
Number 3
Dot notation is a convention to define strings that can specify the position of a value that makes part of a nested array or object.

Using the dot notation can simplify the way you can access the array or objects using strings instead of the usual syntax that PHP provides to access element values that may exist in deep locations of the

This package provides classes with functions that can access array or object elements using dot notation strings to specify the elements' path.

Manuel Lemos
Picture of Smoren  Freelight
  Performance   Level  
Innovation award
Innovation award
Nominee: 16x

 

Documentation

nested-accessor

Packagist PHP Version Support Scrutinizer Code Quality Coverage Status Build and test License: MIT

Accessor for getting and setting values of nested data structures (arrays or objects).

How to install to your project

composer require smoren/nested-accessor

Unit testing

composer install
composer test-init
composer test

Usage

NestedAccessor


use Smoren\NestedAccessor\Components\NestedAccessor;

$source = [
    'data' => [
        'id' => 1,
        'name' => 'Countries classifier',
        'extra' => null,
        'country_names' => ['Russia', 'Belarus'],
    ],
    'countries' => [
        [
            'name' => 'Russia',
            'cities' => [
                [
                    'name' => 'Moscow',
                    'extra' => [
                        'codes' => [
                            ['value' => 7495],
                            ['value' => 7499],
                        ],
                    ],
                ],
                [
                    'name' => 'Petersburg',
                    'extra' => [
                        'codes' => [
                            ['value' => 7812],
                        ],
                    ],
                ],
            ],
        ],
        [
            'name' => 'Belarus',
            'cities' => [
                [
                    'name' => 'Minsk',
                    'extra' => [
                        'codes' => [
                            ['value' => 375017],
                        ],
                    ],
                ],
            ],
        ],
    ]
];

$accessor = new NestedAccessor($input);

echo $accessor->get('data.name'); // 'Countries classifier'
print_r($accessor->get('countries.name')); // ['Russia', 'Belarus']
print_r($accessor->get('countries.cities.name')); // ['Moscow', 'Petersburg', 'Minsk']
print_r($accessor->get('countries.cities.extra.codes.value')); // [7495, 7499, 7812, 375017]

var_dump($accessor->isset('data.name')); // true
var_dump($accessor->isset('data.extra')); // false
var_dump($accessor->isset('this.path.not.exist')); // false

var_dump($accessor->exist('data.name')); // true
var_dump($accessor->exist('data.extra')); // true
var_dump($accessor->exist('this.path.not.exist')); // false

$accessor->set('data.name', 'New name');
echo $accessor->get('data.name'); // 'New name'

$accessor->append('data.country_names', 'Mexico');
echo $accessor->get('data.country_names'); // ['Russia', 'Belarus', 'Mexico']

$accessor->delete('data.name');
var_dump($accessor->exist('data.name')); // false

  Files folder image Files (30)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (5 directories)
Files folder imagetests (3 files, 2 directories)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file codeception.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:58
This week:0
All time:10,551
This week:57Up