Oihana PHP

flatten.php

Table of Contents

Functions

flatten()  : array<string|int, mixed>
Flattens a nested array into a single-level array.

Functions

flatten()

Flattens a nested array into a single-level array.

flatten(array<string|int, mixed> $array) : array<string|int, mixed>

This function recursively iterates through the nested arrays, concatenating all nested values into one flat array.

Parameters
$array : array<string|int, mixed>

The array to flatten.

Tags
example
$nested = [1, [2, 3], [[4], 5], 6];
$flat = flatten($nested);
// Result: [1, 2, 3, 4, 5, 6]

$complex = ['a', ['b', ['c', 'd']], 'e'];
$flat = flatten($complex);
// Result: ['a', 'b', 'c', 'd', 'e']
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
array<string|int, mixed>

The flattened array, containing all nested values in order.


        
On this page

Search results