PHP code example of bonroyage / hierarchy

1. Go to this page and download the library: Download bonroyage/hierarchy library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bonroyage / hierarchy example snippets


new Vosburch\Hierarchy( array $entities, array $branches );

$hierarchy->relatedBy( string|int $branch, array $ids ): array;

$hierarchy->relatedTo( array $ids [, ?array $branches = null] ): array;

$hierarchy = new Vosburch\Hierarchy([
    1 => ['children' => [2, 3]],
    2 => ['parents' => [1], 'children' => [4]],
    3 => ['parents' => [1]],
    4 => ['parents' => [2]],
], ['parents', 'children']);

$hierarchy->relatedBy('parents', [4]); // [1, 2]
$hierarchy->relatedBy('parents', [3]); // [1]
$hierarchy->relatedBy('children', [1]); // [2, 3, 4]

$hierarchy->relatedTo([1]); // [1, 2, 3, 4]
$hierarchy->relatedTo([2]); // [1, 2, 4]

$hierarchy = new Vosburch\Hierarchy([
    'loop1' => ['related' => ['loop2', 'loop3']],
    'loop2' => ['related' => ['loop1', 'loop3']],
    'loop3' => ['related' => ['loop1', 'loop2']],
], ['related']);

$hierarchy->relatedBy('related', ['loop1']); // ['loop2', 'loop3']

$hierarchy->relatedTo(['loop1']); // ['loop1', 'loop2', 'loop3']