Skip to content

Instantly share code, notes, and snippets.

@sweikenb
Created February 5, 2019 14:42
Show Gist options
  • Save sweikenb/1e8dc135a3e0466e15af3aaf5ea173d6 to your computer and use it in GitHub Desktop.
Save sweikenb/1e8dc135a3e0466e15af3aaf5ea173d6 to your computer and use it in GitHub Desktop.
PHP array practice (recursion)
<?php
/*
* Conditions:
***********************************************************************************************************************
*
* Do not modify or edit this file/array -> include it into your working file and use the $tree array as it is.
*
*
* Tasks:
***********************************************************************************************************************
*
* 1. Count the number of nodes of this tree
* 2. Print the tree on the terminal prefixed with a "-" for each level of the corresponding node (example below)
*
*
* Example for taks 2
***********************************************************************************************************************
*
* Node [...]
* - Node [...]
* - Node [...]
* -- Node [...]
* -- Node [...]
* --- Node [...]
* -- Node [...]
* --- Node [...]
* ---- Node [...]
* - Node [...]
* -- Node [...]
* - Node [...]
*
*/
$tree = [
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
[
'name' => 'node-' . __LINE__,
'nodes' => [],
],
],
],
],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment