flat()

The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

Example.php
<?php use kim\present\utils\arrays\ArrayUtils;

$arrayUtils = ArrayUtils::from([
    [1, 2, 3],
    [[4, 5, 6], [7, 8, 9]],
    10
]);

//To flat single level array
$arrayUtils->flat();
// expected output: [1, 2, 3, [4, 5, 6], [7, 8, 9], 10]

//To flat two level array
$arrayUtils->flat(2);
// expected output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Syntax

$arrayUtils->flat(int $dept = 1) : ArrayUtils;

Parameter

    • The depth level that should be flattened. Default is 1.

Return value

  • A new array with the sub-array elements concatenated into it.

Prefixing

$arrayUtils->flatAs(int $dept = 1) : array;
ArrayUtils::flatFrom(iterable $from, int $dept = 1) : ArrayUtils;
ArrayUtils::flatFromAs(iterable $from, int $dept = 1) : array;

References

Last updated