Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8a3b0ed

Browse files
committed
feat: arr - add flatten deep array to simple map
1 parent 09ec9a0 commit 8a3b0ed

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

‎src/Arr/ArrayHelper.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,51 @@ public static function toLimitOut($array): array
883883
return $array;
884884
}
885885

886+
public const FLAT_DOT_JOIN_INDEX = 1;
887+
888+
/**
889+
* @param iterable $data
890+
* @param int $flags
891+
*
892+
* @return array
893+
*/
894+
public static function flattenMap(iterable $data, int $flags = 0): array
895+
{
896+
$flatMap = [];
897+
self::doFlattenMap($data, $flatMap, '', $flags);
898+
return $flatMap;
899+
}
900+
886901
/**
902+
* @param iterable $data
903+
* @param array $flatMap
904+
* @param string $parentPath
905+
* @param int $flags
906+
*/
907+
public static function doFlattenMap(iterable $data, array &$flatMap = [], string $parentPath = '', int $flags = 0): void
908+
{
909+
foreach ($data as $key => $val) {
910+
if ($parentPath) {
911+
if (is_int($key) && $flags^self::FLAT_DOT_JOIN_INDEX) {
912+
$path = $parentPath . "[$key]";
913+
} else {
914+
$path = $parentPath . '.' . $key;
915+
}
916+
} else {
917+
$path = $key;
918+
}
919+
920+
if (is_scalar($val) || $val === null) {
921+
$flatMap[$path] = $val;
922+
} else {
923+
self::doFlattenMap($val, $flatMap, $path, $flags);
924+
}
925+
}
926+
}
927+
928+
/**
929+
* Quickly build multi line k-v text
930+
*
887931
* @param array $data
888932
* @param string $kvSep
889933
* @param string $lineSep

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /