Skip to content

Commit

Permalink
Update Array.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenglish7 committed Jan 27, 2018
1 parent cb6f21e commit 2afb5e7
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/Function/Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,37 @@
* Copyright (c) 2018 Nicholas English <https://github.com/Nenglish7>.
*/

namespace Nenglish\Function\Array;
namespace Nenglish7\Function\Array;

/**
* array_depth().
*
* Get current depth of an array.
*
* @param array $array The array to test
*
* @throws InvalidArgumetException If `$array` is not an array.
*
* @return int Return the current depth of the array passed.
*/
function array_depth($array)
{
if (!\is_array($array)) {
throw new Exception\InvalidArgumetException(\sprintf(
'`$array` has to be an array data type. passed: `%s`.',
\htmlspecialchars(\var_export(\serialize($array), \true), \ENT_QUOTES, 'UTF-8')
));
}
$depth = 1;
if (empty($array)) {
goto finish;
}
foreach ($array as $value) {
if (\is_array($value)) {
$depth += array_depth($value);
break;
}
}
finish:
return $depth;
}

0 comments on commit 2afb5e7

Please sign in to comment.