PHP bundle which makes array traversing / inserting dead easy.

Overview

XTraverse.php

This bundle makes it dead easy to traverse through nested arrays/objects in PHP.

Installation

Via Composer

composer require phiil/xtraverse

File download

To use all the functions of this package simply download the src/ directory.

Getting started

Traversing paths

Paths are how you specify where you want to traverse to inside the nested object. Let's assume you want to get the title of the Block with ID 1 inside the 'blocks' array.

Our nested object:

$data = [
    'blocks' => [
        [
            'id' => 1,
            'title' => 'First block',
        ],
    ],
];

Now we specify which element we want:

use Phiil\XTraverse\Service\TraverseService;

$path = 'blocks[1].title';

$traverseService = new TraverseService();
$nodes = $traverseService->getNodes($path); // this function will split up the path by its steps (explode by '.')

Every step is delimited with a dot ('.').
If you want to query for an ID simply append it to the path with closed brackets.

Now let the traversing begin:

use Phiil\XTraverse\Service\TraverseService;

$traverseService = new TraverseService();
$title = $traverseService->traverseData($nodes, $data, false); // we want a non-array value - pass false or the service will throw an exception

echo $title;

The above will output the following: First block

Updating a nested value

Updating a value also works with paths:

use Phiil\XTraverse\Service\TraverseService;

// we want to update the title of the block we previously traversed to
$updatePath = 'blocks[1].title';

$traverseService = new TraverseService();
$data = $traverseService->update($data, $path, 'New title')->data;

Note: The update method returns an object with the properties path, data & insert. Getting the data property from the object straight away is almost always the best option.

Working with IDs

The traverse service can auto-increment IDs - meaning if you insert a nested object like:

use Phiil\XTraverse\Service\TraverseService;

$object = [
    'id' => null,
    'title' => 'Second block',
];
$traverseService = new TraverseService();
$data = $traverseService->update($object, 'blocks.$', $object)->data;

The object inside $data will now have the ID of 2 (First Block: ID 1).

Note: The path.$ can be used if you want to add a block to a non-associative (only numeric keys) array.

Running tests

To run tests run the following commands:

composer install --dev
./vendor/bin/phpunit tests/

Problems? Issues?

Just post them here on Github or contact me via email: [email protected]. Feel free to contribute!

You might also like...
Scotch Box is a preconfigured Vagrant Box with a full array of LAMP Stack features to get you up and running with Vagrant in no time.
Scotch Box is a preconfigured Vagrant Box with a full array of LAMP Stack features to get you up and running with Vagrant in no time.

Scotch Box is a preconfigured Vagrant Box with a full array of LAMP Stack features to get you up and running with Vagrant in no time.

Your alter ego object. Takes the best of object and array worlds.

Supporting Opensource formapro\values is an MIT-licensed open source project with its ongoing development made possible entirely by the support of com

Miniset allows you to create compact sets of fields that either combine into a string of classes, or return a simple array of values

Miniset allows you to create compact sets of fields that either combine into a string of classes, or return a simple array of values. Miniset

Fornece acesso de leitura ou gravação para o array através de notação de ponto

PHP Dot Notation Fornece acesso de leitura ou gravação para o array através de notação de ponto Lendo valores: Permite que você use uma sintaxe separa

PHP Unoconv - An Object Oriented library which allow easy to use file conversion with Unoconv.

An Object Oriented library which allow easy to use file conversion with Unoconv. Install The recommended way to install PHP-Unoconv is thr

Formcreator is a plugin which allow creation of custom forms of easy access
Formcreator is a plugin which allow creation of custom forms of easy access

Formcreator is a plugin which allow creation of custom forms of easy access. At the same time, the plugin allow the creation of one or more tickets when the form is filled.

NamelessMC is a free, easy to use & powerful website software for your Minecraft server, which includes a large range of features.
NamelessMC is a free, easy to use & powerful website software for your Minecraft server, which includes a large range of features.

NamelessMC is a free, easy to use & powerful website software for your Minecraft server, which includes a large range of features

This package is used to validate the telephone numbers of the countries taken into account. It also makes it possible to verify that a number is indeed a number of an operator X

phone-number-checker This package is used to validate the telephone numbers of the countries taken into account. It also makes it possible to verify t

WordPlate is a wrapper around WordPress. It makes developers life easier. It is just like building any other WordPress website with themes and plugins. Just with sprinkles on top.
WordPlate is a wrapper around WordPress. It makes developers life easier. It is just like building any other WordPress website with themes and plugins. Just with sprinkles on top.

WordPlate is simply a wrapper around WordPress. It makes developers life easier. It is just like building any other WordPress website with themes and plugins. Just with sprinkles on top.

Releases(1.1)
Owner
Philipp Martini
Backend-Developer @ Riddle. Studying Computer Science at the University Of London
Philipp Martini
Dead Code Detector (DCD) for PHP code.

This project is no longer maintained and its repository is only kept for archival purposes. PHP Dead Code Detector (PHPDCD) phpdcd is a Dead Code Dete

Sebastian Bergmann 406 Dec 30, 2022
Magento extension which makes it impossible for a customer to log in until the account has been activated by the administrator.

This Magento 1 extension is orphaned, unsupported and no longer maintained. If you use it, you are effectively adopting the code for your own project.

Vinai Kopp 120 Oct 10, 2022
WARNING! This software is currently non-functional. - A system which makes installing Jexactyl far, far easier.

Jexactyl Assistant A system which makes installing Jexactyl far, far easier. WARNING ?? This software is currently in heavy alpha testing and WILL NOT

Jexactyl 7 Nov 14, 2022
RRR makes structured data for WordPress really rich, and really easy.

Really Rich Results - JSON-LD Structured Data (Google Rich Results) for WordPress Search engines are putting more weight on structured data than ever

Pagely 22 Dec 1, 2022
This package makes it easy to add early access mode to your existing application.

This package makes it easy to add early access mode to your existing application. This is useful for when you want to launch a product and need to gat

Neo 174 Nov 26, 2022
ConFOMO is a simple tool that makes it easy to track your friends at conferences.

Connecting your online community with the real world, one conference at a time. Built in 4 hours to help me track who I wanted to meet at Laracon 2014

Tighten 74 Jul 20, 2022
A Symfony Feature Flag Bundle which easily allows you to configure and use your favorite feature flag provider.

Metro Markets FF Metro Markets FF is a Feature Flag Symfony Bundle. It easily allows you to configure and use your favorite feature flag provider. Ins

METRO Markets 14 May 23, 2022
Tabler.io bundle for Symfony - a backend/admin theme for easy integration

Tabler Bundle for Symfony This repository contains a Symfony bundle, integrating the fantastic Tabler.io HTML Template into your Symfony project. It s

Kevin Papst 22 Jan 2, 2023
World Currency list in PHP constants and in array (Currency::USD)

World Currency list in PHP constants and in array (Currency::USD) If you need to work with currencies in the code and describe each time "USD", "EUR"

Krepysh 4 Jun 27, 2022
A tidy conditional Blade directive for checking if something is in an array

A tidy conditional Blade directive for checking if something is in an array. This package provides a small @in directive that allows you to simplify i

Ryan Chandler 0 Oct 7, 2022