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]
$arrayUtils->flat(int $dept = 1) : ArrayUtils;
$dept
- The depth level that should be flattened. Default is
1
.
- A new array with the sub-array elements concatenated into it.
$arrayUtils->flatAs(int $dept = 1) : array;
ArrayUtils::flatFrom(iterable $from, int $dept = 1) : ArrayUtils;
ArrayUtils::flatFromAs(iterable $from, int $dept = 1) : array;
Last modified 2yr ago