Skip to content

Commit 5165980

Browse files
committed
Added isNested and getDepth methods
1 parent f1e02db commit 5165980

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

README.md

+36
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Here, you can quickly get started by becoming familiar with each and every metho
5252
* [isAssoc](#isassocarray-array-bool-strict--false-bool)
5353
* [isNumeric](#isnumericarray-array-bool)
5454
* [isUnique](#isuniquearray-array-bool-strict--false-bool)
55+
* [isNested](#isnestedarray-array-bool)
5556
* [isArrayOfArrays](#isarrayofarraysarray-array-bool)
5657
### Manipulation
5758
* Mapping
@@ -77,6 +78,7 @@ Here, you can quickly get started by becoming familiar with each and every metho
7778
### Utilities
7879
* [createMulti](#createmultiarray-keys-array-values--null-array)
7980
* [forceArray](#forcearraymixed-var-int-flag--selfforce_array_all-mixed)
81+
* [getDepth](#getdeptharray-array-int)
8082
* [clone](#clonearray-array-array)
8183
* [random](#randomarray-array-int-count--1-mixed)
8284
* [shuffle](#shufflearray-array-array)
@@ -265,6 +267,19 @@ Arr::isUnique([1, '1', true]) -> false
265267
Arr::isUnique([1, '1', true], true) -> true
266268
```
267269

270+
### `isNested(array $array): bool`
271+
Check if any element of an array is also an array
272+
273+
```php
274+
Arr::isNested([]) -> false
275+
276+
Arr::isNested([1, 2, 3]) -> false
277+
278+
Arr::isNested([1, 2 => [], 3]) -> true
279+
280+
Arr::isNested([1, 2 => [[[]]], 3 => []]) -> true
281+
```
282+
268283
### `isArrayOfArrays(array $array): bool`
269284
Check if every array element is array
270285

@@ -826,6 +841,27 @@ Arr::forceArray($object) -> [$object]
826841
Arr::forceArray($object, Arr::FORCE_ARRAY_PRESERVE_ARRAY_OBJECTS) -> $object
827842
```
828843

844+
### `getDepth(array $array): int`
845+
Get nesting depth of an array
846+
847+
```php
848+
Arr::getDepth([]) -> 1
849+
850+
Arr::getDepth([1, 2, 3]) -> 1
851+
852+
Arr::getDepth([1, 2 => [], 3]) -> 2
853+
854+
Arr::getDepth([
855+
1,
856+
2 => [
857+
3 => [
858+
4 => []
859+
]
860+
],
861+
5 => []
862+
]) -> 4
863+
```
864+
829865
### `clone(array $array): array`
830866
Copy array and clone every object inside it
831867

src/Arr.php

+48-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,24 @@ public static function isUnique(array $array, bool $strict = false): bool
305305
}
306306

307307
/**
308-
* Check if every array element is array
308+
* Check if any element of an array is also an array
309+
*
310+
* @param array $array
311+
* @return bool
312+
*/
313+
public static function isNested(array $array): bool
314+
{
315+
foreach ($array as $element) {
316+
if (is_array($element)) {
317+
return true;
318+
}
319+
}
320+
321+
return false;
322+
}
323+
324+
/**
325+
* Check if every element of an array is array
309326
*
310327
* @param array $array
311328
* @return bool
@@ -782,6 +799,36 @@ public static function forceArray($var, int $flag = self::FORCE_ARRAY_ALL)
782799
}
783800

784801

802+
/**
803+
* Get nesting depth of an array.<br>
804+
* <br>
805+
* Depth is calculated by counting amount of nested arrays - each nested array increase depth by one.
806+
* Nominal depth of an array is 1.
807+
*
808+
* @param array $array
809+
* @return int
810+
*/
811+
public static function getDepth(array $array): int
812+
{
813+
$depth = 0;
814+
$queue = [$array];
815+
816+
do {
817+
++$depth;
818+
$current = $queue;
819+
$queue = [];
820+
foreach ($current as $element) {
821+
foreach ($element as $value) {
822+
if (is_array($value)) {
823+
$queue[] = $value;
824+
}
825+
}
826+
}
827+
} while(!empty($queue));
828+
829+
return $depth;
830+
}
831+
785832
/**
786833
* Copy array and clone every object inside it
787834
*

test/ArrTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ public function testIsUnique()
238238
$this->assertFalse(Arr::isUnique([1, 1, 1], true));
239239
}
240240

241+
public function testIsNested()
242+
{
243+
$this->assertFalse(Arr::isNested([]));
244+
$this->assertFalse(Arr::isNested([1, 2, 3, 'a', 'b']));
245+
$this->assertTrue(Arr::isNested([[], []]));
246+
$this->assertTrue(Arr::isNested([[]]));
247+
$this->assertTrue(Arr::isNested([1, 2 => []]));
248+
}
249+
241250
public function testIsArrayOfArrays()
242251
{
243252
$this->assertFalse(Arr::isArrayOfArrays([]));
@@ -983,6 +992,33 @@ public function testForceArray()
983992
$this->assertSame([$function], Arr::forceArray($function));
984993
}
985994

995+
public function testGetDepth()
996+
{
997+
$this->assertSame(1, Arr::getDepth([]));
998+
$this->assertSame(1, Arr::getDepth([1, 2, 3, 4, 'a', 'b', 'c']));
999+
$this->assertSame(2, Arr::getDepth([[]]));
1000+
$this->assertSame(5, Arr::getDepth([
1001+
[],
1002+
'a' => [
1003+
'b' => [
1004+
'c' => 2,
1005+
'd' => [
1006+
[],
1007+
'e' => 'test',
1008+
],
1009+
[
1010+
'f' => []
1011+
]
1012+
]
1013+
],
1014+
10,
1015+
'foo' => [
1016+
'bar',
1017+
[]
1018+
]
1019+
]));
1020+
}
1021+
9861022
public function testClone()
9871023
{
9881024
$object = new stdClass();

0 commit comments

Comments
 (0)