deep array - 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 deep array.php

  1. <?php
  2. class FooBar
  3. {
  4.     function foo_function()
  5.     {
  6.         return "Hello World!";
  7.     }
  8. }
  9.  
  10. $var_class = new FooBar;
  11.  
  12. $example_array = [                                                  // deep = 1
  13.     'null'         => null,
  14.     'null_text'    => 'null',
  15.     'integer'      => 10,
  16.     'integer_text' => '10',
  17.     'float'        => 20.35,
  18.     'float_text'   => '20.35',
  19.     'string'       => 'Hello World',
  20.     'date_1'       => '2021-01-17',
  21.     'date_2'       => '2021-Jan-17',
  22.     'hour_1'       => '6:31:00 AM',
  23.     'hour_2'       => '17:31:00',
  24.     'datetime_1'   => '2021-01-17 17:31:00',
  25.     'datetime_2'   => '2021-Jan-17 6:31:00 AM',
  26.     'datetime_3'   => '2021-01-17 6:31:00 AM',
  27.     'datetime_4'   => '2021-Jan-17 17:31:00',
  28.     'currency_1'   => '1.45$',
  29.     'currency_2'   => '$ 1.45',
  30.     'array'        => [                                             // deep = 2
  31.         'boolean_true'       => true,
  32.         'boolean_false'      => false,
  33.         'boolean_true_text'  => 'true',
  34.         'boolean_false_text' => 'false',
  35.         'object'             => (object) [                          // deep = 3
  36.             'key_index_most_highed_of_the_example' => 'Hello Wolrd,Hello Wolrd,Hello Wolrd,Hello Wolrd',
  37.             'joder'                                => [             // deep = 4 ----Este es el maximo----
  38.                 'prueba' => 'prueba',
  39.             ]
  40.         ],
  41.     ],
  42.     'objects_list' => [                                             // deep = 2 no cuenta ya existe
  43.         'object_empty' => (object) [],                              // deep = 3 no cuenta ya existe
  44.         'class'        => $var_class,
  45.     ],
  46. ];
  47.  
  48. $example_array['array']['nested'] = ['other' => (object) ['apple', 'banana', 'coconut']];
  49.  
  50.  
  51. function calcDeepArray($array)
  52.     {
  53.         $max_depth = 1;
  54.         foreach ($array as $key => $value) {
  55.             if (is_array($value)) {
  56.                 $depth = calcDeepArray($value) + 1;
  57.                 if ($depth > $max_depth) {
  58.                     echo $key . '<br>';
  59.                     $max_depth = $depth;
  60.                 }
  61.             }
  62.         }
  63.         return $max_depth;
  64.     }
  65.  
  66.  
  67. array_walk_recursive($example_array, function (&$value, $key) {
  68.     $value = is_object($value) ? (array) $value : $value;
  69. });
  70.  
  71. calcDeepArray($example_array);
  72.  
File Description
  • deep array
  • PHP Code
  • 22 Feb-2021
  • 2.24 Kb
You can Share it: