Minwork Array
  • Minwork Array
  • Common methods
    • has
    • get → getNestedElement
    • set → setNestedElement
    • remove
    • clone
    • getKeysArray
  • Object oriented methods
    • General information
  • Traversing array
    • Finding
    • Iterating
  • Manipulating array
    • Mapping
    • Filtering
    • Grouping
    • Sorting
    • Computations
    • Flattening
  • Validating array
    • check
    • isEmpty
    • isNested
    • isArrayOfArrays
    • isAssoc
    • isUnique
    • isNumeric
    • hasKeys
  • Utility methods
    • pack
    • unpack
    • createMulti
    • forceArray
    • getDepth
    • random
    • shuffle
    • nth
    • getFirstKey
    • getLastKey
    • getFirstValue
    • getLastValue
Powered by GitBook
On this page
  • flatten
  • flattenSingle

Was this helpful?

  1. Manipulating array

Flattening

flatten

Definition

Arr::flatten(array $array, ?int $depth = null, bool $assoc = false): array

Description

Flatten array of arrays to a n-depth array

Examples

$array = [
    'a' => [
        'b' => [
            'c' => 'test'
        ],
        'd' => 1
    ],
    'b' => [
        'e' => 2
    ]
];

Arr::flatten($array) -> 
[
    'test', 
    1, 
    2
]
Arr::flatten($array, 1) -> 
[
    ['c' => 'test'], 
    1, 
    2
]
Arr::flatten($array, 0) -> 
[
    [
        'b' => [
            'c' => 'test'
        ],
        'd' => 1
    ],
    [
        'e' => 2
    ]
]

Arr::flatten([[[[]]]]) -> []

// When $assoc is set to true this method will try to preserve as much string keys as possible using automatically generated numeric indexes as fallback
Arr::flatten($array, null, true) -> 
[
    'c' => 'test', 
    'd' => 1, 
    'e' => 2
]

$array = [
    'a' => [
        'b' => [
            'c' => 1,
        ],
    ],
    [
        'c' => 2
    ]
];

// Here key 'c' is duplicated so it will fallback to numeric index
Arr::flatten($array, null, true) ->
[
    'c' => 1,
    2,
]

flattenSingle

Definition

Arr::flattenSingle(array $array): array

Description

Flatten single element arrays (also nested single element arrays)

Examples

$array = [
    'a' => ['test'],
    'b' => [
        'test2',
        'c' => ['test3']
    ]
];

Arr::flattenSingle($array) ->
[
    'a' => 'test',
    'b' => [
        'test2',
        'c' => 'test3'
    ],
]

$array = [
    'a' => [
        'b' => 1
    ],
    'b' => 2,
];

Arr::flattenSingle($array) -> ['a' => 1, 'b' => 2]

Arr::flattenSingle([['a']]) -> ['a']

Arr::flattenSingle([]) -> []
PreviousComputationsNextcheck

Last updated 5 years ago

Was this helpful?