mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
[Arrays] Allow to define a key of next level elements in a function that returns elements from given level
This commit is contained in:
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
Common and useful classes, methods, exceptions etc.
|
Common and useful classes, methods, exceptions etc.
|
||||||
|
|
||||||
|
# 1.1.7
|
||||||
|
|
||||||
|
1. [Arrays] Allow to define a key of next level elements in a function that returns elements from given level
|
||||||
|
|
||||||
# 1.1.6
|
# 1.1.6
|
||||||
|
|
||||||
1. [Arrays] Function that returns elements from given level
|
1. [Arrays] Function that returns elements from given level
|
||||||
|
|||||||
@@ -1642,7 +1642,7 @@ class Arrays
|
|||||||
return '' === trim(implode('', $array));
|
return '' === trim(implode('', $array));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getElementsFromLevel(array $array, int $level): ?array
|
public static function getElementsFromLevel(array $array, int $level, ?string $childrenKey = null): ?array
|
||||||
{
|
{
|
||||||
if (empty($array) || $level <= 0) {
|
if (empty($array) || $level <= 0) {
|
||||||
return null;
|
return null;
|
||||||
@@ -1650,11 +1650,16 @@ class Arrays
|
|||||||
|
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
foreach ($array as $value) {
|
foreach ($array as $key => $value) {
|
||||||
// This is the expected level (the deepest). Comparing with 1, because level will be decreased by 1, and
|
// This is the expected level (the deepest). Comparing with 1, because level will be decreased by 1 (later),
|
||||||
// finally we will get the latest/deepest level that equals 1.
|
// and finally we will get the latest/deepest level that equals 1.
|
||||||
if ($level === 1) {
|
if ($level === 1) {
|
||||||
$result[] = $value;
|
// No key of children (next level) provided or this is the same key as processed?
|
||||||
|
// We've got the expected value
|
||||||
|
if ($childrenKey === null || $key === $childrenKey) {
|
||||||
|
$result[] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1663,8 +1668,8 @@ class Arrays
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Let's dive one level down
|
// Let's dive one level down/deeper
|
||||||
$elements = self::getElementsFromLevel($value, $level - 1);
|
$elements = self::getElementsFromLevel($value, $level - 1, $childrenKey);
|
||||||
|
|
||||||
if ($elements === null) {
|
if ($elements === null) {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -1645,6 +1645,73 @@ letsTest[2] = value_2;';
|
|||||||
self::assertSame($expectedLevel3, Arrays::getElementsFromLevel($array, 3));
|
self::assertSame($expectedLevel3, Arrays::getElementsFromLevel($array, 3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetElementsFromLevelIfGivenKeyDoesNotExist(): void
|
||||||
|
{
|
||||||
|
$array = [
|
||||||
|
'test1' => [1, 2, 3],
|
||||||
|
'test2' => [4, 5, 6],
|
||||||
|
'test3' => [
|
||||||
|
'xy',
|
||||||
|
'test4' => [7, 8, 9],
|
||||||
|
'test5' => [
|
||||||
|
'test6' => [10, 11, 12],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
self::assertSame([], Arrays::getElementsFromLevel($array, 2, 'X'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetElementsFromLevelWithGivenKey(): void
|
||||||
|
{
|
||||||
|
$array = [
|
||||||
|
// Level 1:
|
||||||
|
[
|
||||||
|
'a',
|
||||||
|
'b',
|
||||||
|
|
||||||
|
// Level 2:
|
||||||
|
'c' => [
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
'c' => [
|
||||||
|
4,
|
||||||
|
5,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
|
||||||
|
// Level 1:
|
||||||
|
[
|
||||||
|
'd',
|
||||||
|
'e',
|
||||||
|
|
||||||
|
// Level 2:
|
||||||
|
'c' => [
|
||||||
|
6,
|
||||||
|
7,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$expected = [
|
||||||
|
[
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
'c' => [
|
||||||
|
4,
|
||||||
|
5,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
6,
|
||||||
|
7,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
self::assertSame($expected, Arrays::getElementsFromLevel($array, 2, 'c'));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides simple array to set/replace values with keys
|
* Provides simple array to set/replace values with keys
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user