Can you explain the algorithm provided in the forum thread for determining array dimensions in PHP?
The algorithm provided in the forum thread aims to determine the dimensions of a multidimensional array in PHP. This can be achieved by recursively iterating through the array and counting the number of elements at each level. By keeping track of the maximum count encountered at each level, we can determine the dimensions of the array.
function getArrayDimensions($array) {
if (!is_array($array)) {
return false;
}
$dimensions = [];
$dimensions[] = count($array);
if (is_array($array[0])) {
$subDimensions = getArrayDimensions($array[0]);
$dimensions = array_merge($dimensions, $subDimensions);
}
return $dimensions;
}
// Example usage
$array = [[1, 2], [3, 4, 5]];
$dimensions = getArrayDimensions($array);
print_r($dimensions);
Keywords
Related Questions
- Are there any specific best practices for discussing CSS-related questions in forums like php.de?
- What are the potential pitfalls of compiling PHP with specific modules on Windows?
- What are the common pitfalls when using multiple database queries in PHP, especially when trying to retrieve and display related data?