Skip to content

array_flatten

Tonya Mork edited this page Dec 11, 2017 · 5 revisions

The array_flatten() function flattens a multi-dimensional array into a single level (depth). By default, the entire array, meaning all depths, are flattened. However, you can specify the number of depths.

Note: The keys are not preserved in the flattened level.

Syntax

array array_flatten(
   array $subjectArray, 
   int $depth = INF
);

where,

$subjectArray is the array to work on

$depth is the depth to flatten, meaning the number of levels within the $subjectArray to flatten. It defaults to infinite, meaning the entire array.

Example

$user = [
	'user_id'   => 504,
	'name'      => 'Bob Jones',
	'social'    => [
		'twitter' => '@bobjones',
	],
];

$user = array_flatten($user);

/**
$user = [504, 'Bob Jones', '@bobjones'];
*/
Clone this wiki locally