3456789087654 - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of 3456789087654.php

  1. <?php
  2. error_reporting(E_ERROR);
  3. $tree = '{"name": "A","items" : [{"name": "B","items" : [{"name": "D"},{"name": "E","items" : [{"name": "G"}]}]},{"name": "C","items" : [{"name": "F","items" : [{"name": "H"},{"name": "I"}]}]}]}';
  4. $tree = json_decode($tree, 1);
  5.  
  6. treeTraversal($tree);
  7.  
  8. function treeTraversal(&$tree)
  9. {
  10.     if (!$tree['items']) {
  11.         return;
  12.     }
  13.  
  14.     $tree['children'] = count($tree['items']);
  15.  
  16.     foreach ($tree['items'] as &$item) {
  17.         $item['count'] = treeTraversal($item);
  18.     }
  19.  
  20.     $tree['count'] = array_sum(array_column($tree['items'], 'count')) + $tree['children'];
  21.  
  22.     return $tree['count'];
  23.  
  24. }
  25.  
  26. print_r($tree);
  27.  
File Description
  • 3456789087654
  • PHP Code
  • 21 Oct-2021
  • 649 Bytes
You can Share it: