Skip to content

Commit

Permalink
chore: fix spacing issue with flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
borkweb committed Dec 18, 2024
1 parent 880c931 commit ae93cde
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/Arrays/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,37 +379,37 @@ public static function first( $array, callable $callback = null, $default = null
* @return array The flattened array.
*/
public static function flatten( $array, int $depth = PHP_INT_MAX ): array {
$result = [];
$result = [];

if ( $depth < 1 ) {
return $array;
}
if ( $depth < 1 ) {
return $array;
}

foreach ( $array as $key => $item ) {
if ( ! is_array( $item ) ) {
// Preserve string keys, use numeric keys for numeric
if ( is_string( $key ) ) {
$result[ $key ] = $item;
} else {
$result[] = $item;
}
} else {
$values = $depth === 1
? array_values( $item )
: static::flatten( $item, $depth - 1 );

foreach ( $values as $value_key => $value ) {
// Preserve string keys from nested arrays
if ( is_string( $value_key ) ) {
$result[ $value_key ] = $value;
foreach ( $array as $key => $item ) {
if ( ! is_array( $item ) ) {
// Preserve string keys, use numeric keys for numeric
if ( is_string( $key ) ) {
$result[ $key ] = $item;
} else {
$result[] = $value;
$result[] = $item;
}
} else {
$values = $depth === 1
? array_values( $item )
: static::flatten( $item, $depth - 1 );

foreach ( $values as $value_key => $value ) {
// Preserve string keys from nested arrays
if ( is_string( $value_key ) ) {
$result[ $value_key ] = $value;
} else {
$result[] = $value;
}
}
}
}
}

return $result;
return $result;
}

/**
Expand Down

0 comments on commit ae93cde

Please sign in to comment.