How can a nested array structure be converted into a simple array structure in PHP?
To convert a nested array structure into a simple array structure in PHP, you can use recursion to flatten the array. This involves iterating through the nested arrays and adding their elements to a new simple array. By recursively flattening the nested arrays, you can create a single-level array with all the elements.
function flattenArray($array) {
$result = [];
foreach ($array as $element) {
if (is_array($element)) {
$result = array_merge($result, flattenArray($element));
} else {
$result[] = $element;
}
}
return $result;
}
$nestedArray = [1, 2, [3, 4, [5, 6]], 7];
$simpleArray = flattenArray($nestedArray);
print_r($simpleArray);