compact() Is Recursive

<?php

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

$d = compact($a);
print_r($d);

?>

compact() accepts an array of strings… and arrays. It uses the available arguments recursively, yet produces an array with only one level: the deep nesting is lots at compacting type.

In fact, if the variables are indeed available at compact() time, compact flattens the array, and turns it into a simple list.

It also means that it is prone to recursive error.

See Also

PHP Error Messages

PHP Features

Last updated: 14 July 2026