2017 © Pedro Peláez
 

library array-tree-walker

Very easy way to parse arrays with tree structure

image

rgilyov/array-tree-walker

Very easy way to parse arrays with tree structure

  • Wednesday, August 2, 2017
  • by RomaGilyov
  • Repository
  • 1 Watchers
  • 0 Stars
  • 52 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 8 % Grown

The README.md

ArrayTreeWalker

Installation

composer require rgilyov/array-tree-walker dev-master

Description

Lets make an example of array with tree structure:, (*1)

$tree = [
    'name'    => 'child',
    'parents' => [
        'mother' => [
            'name'    => 'mother',
            'parents' => [
                'mother' => [
                    'name' => 'first grand mother'
                ],
                'father' => [
                    'name' => 'first grand father'
                ]
            ]
        ],
        'father' => [
            'name'    => 'father',
            'parents' => [
                'mother' => [
                    'name' => 'second grand mother'
                ],
                'father' => [
                    'name'    => 'second grand father',
                    'parents' => [
                        'mother' => [
                            'name' => 'second grand grand mother'
                        ]
                    ]
                ]
            ]
        ]
    ]
];

$walker = new \RGilyov\ArrayTreeWalker($tree);

To walk through tree structure use ->, to get value use ->get('name');, (*2)

echo $walker->parents->mother->parents->father->get('name'); // result: `first grand father`

You can also get value like so, (*3)

echo $walker->parents->mother->parents->father['name']; // result: `first grand father`

echo $walker->parents->mother->parents->father->name['name']; // result: `first grand father`

echo $walker->parents->mother->parents->father->name->father['name']; // result: null

You may also specify node name, just pass it as the second the class's constructor parameter, (*4)

$walkerWithNodeName = new \RGilyov\ArrayTreeWalker($tree, 'parents');

echo $walkerWithNodeName->father->father->get('name'); // result: `second grand father`

echo $walkerWithNodeName->father->father->mother['name']; // result: 'second grand grand mother'

echo $walkerWithNodeName->father->father->mother->mother['name']; // result: null

You also can walk down the tree and walk up the tree and modify each node during walking:, (*5)


$walker = new \RGilyov\ArrayTreeWalker($tree, 'parents'); /* * If an array will be returned the values for given keys will be rewritten and if keys didn't exist in the node they will be attached */ $walker->walkDown(function ($node, $level, $nodeNumber, $parentNodeNumber) { return [ 'level' => $level, 'node_number' => $nodeNumber, 'parent_node_number' => $parentNodeNumber ]; }); $walker->walkUp(function ($node, $nodeChildren, $parameters, $level, $nodeNumber) { return [ 'super_name' => $node['name'] . ' ' . implode(array_map(function ($child) { return $child['name']; }, $nodeChildren), ' ') ]; });

List of available methods:, (*6)

$walker->toArray();
$walker->count();
$walker->offsetExists('key');
$walker->offsetUnset('key');
$walker->offsetGet('key');
$walker->offsetSet('key', 'value');

The Versions

02/08 2017

dev-master

9999999-dev

Very easy way to parse arrays with tree structure

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Roman Gilyov

tree parse nested array

02/08 2017

1.0.0

1.0.0.0

Very easy way to parse arrays with tree structure

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Roman Gilyov

tree parse nested array