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.
Result of php executing
Full code of deep array.php
- <?php
- class FooBar
- {
- function foo_function()
- {
- return "Hello World!";
- }
- }
- $var_class = new FooBar;
- $example_array = [ // deep = 1
- 'null' => null,
- 'null_text' => 'null',
- 'integer' => 10,
- 'integer_text' => '10',
- 'float' => 20.35,
- 'float_text' => '20.35',
- 'string' => 'Hello World',
- 'date_1' => '2021-01-17',
- 'date_2' => '2021-Jan-17',
- 'hour_1' => '6:31:00 AM',
- 'hour_2' => '17:31:00',
- 'datetime_1' => '2021-01-17 17:31:00',
- 'datetime_2' => '2021-Jan-17 6:31:00 AM',
- 'datetime_3' => '2021-01-17 6:31:00 AM',
- 'datetime_4' => '2021-Jan-17 17:31:00',
- 'currency_1' => '1.45$',
- 'currency_2' => '$ 1.45',
- 'array_2' => [ // deep = 2
- 'boolean_true' => true,
- 'boolean_false' => false,
- 'boolean_true_text' => 'true',
- 'boolean_false_text' => 'false',
- 'object' => (object) [ // deep = 3
- 'key_index_most_highed_of_the_example' => 'Hello Wolrd,Hello Wolrd,Hello Wolrd,Hello Wolrd',
- 'joder' => [ // deep = 4 ----Este es el maximo----
- 'prueba' => 'prueba',
- ]
- ],
- 'nested' => [ // deep = 3 no cuenta ya existe
- 'other_obj' => (object) [ // deep = 4 no cuenta ya existe
- 'apple',
- 'banana',
- 'coconut'
- ],
- ],
- ],
- 'objects_list' => [ // deep = 2 no cuenta ya existe
- 'object_empty' => (object) [], // deep = 3 no cuenta ya existe
- 'class' => $var_class,
- ],
- ];
- function calcDeepArray($array)
- {
- $max_depth = 1;
- foreach ($array as $key => $value) {
- if (is_array($value)) {
- $depth = calcDeepArray($value) + 1;
- if ($depth > $max_depth) {
- echo $key . '<br>';
- $max_depth = $depth;
- }
- }
- }
- return $max_depth;
- }
- ksort($example_array);
- array_walk_recursive($example_array, function (&$value, $key) {
- $value = is_object($value) ? (array) $value : $value;
- });
- echo calcDeepArray($example_array);
File Description
- deep array
- PHP Code
- 22 Feb-2021
- 2.48 Kb
You can Share it:
Latest PHP Pastes