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_2'        => [                                             // 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.         'nested' => [                                               // deep = 3 no cuenta ya existe
  42.             'other_obj' => (object) [                               // deep = 4 no cuenta ya existe
  43.                 'apple', 
  44.                 'banana', 
  45.                 'coconut'
  46.             ],
  47.         ],
  48.     ],
  49.     'objects_list' => [                                             // deep = 2 no cuenta ya existe
  50.         'object_empty' => (object) [],                              // deep = 3 no cuenta ya existe
  51.         'class'        => $var_class,
  52.     ],
  53. ];
  54.  
  55.  
  56. function calcDeepArray($array)
  57.     {
  58.         $max_depth = 1;
  59.         foreach ($array as $key => $value) {
  60.             if (is_array($value)) {
  61.                 $depth = calcDeepArray($value) + 1;
  62.                 if ($depth > $max_depth) {
  63.                     echo $key . '<br>';
  64.                     $max_depth = $depth;
  65.                 }
  66.             }
  67.         }
  68.         return $max_depth;
  69.     }
  70.  
  71. ksort($example_array);
  72. array_walk_recursive($example_array, function (&$value, $key) {
  73.     $value = is_object($value) ? (array) $value : $value;
  74. });
  75.  
  76. echo calcDeepArray($example_array);
  77.  
File Description
  • deep array
  • PHP Code
  • 22 Feb-2021
  • 2.48 Kb
You can Share it: