Last active
November 28, 2016 09:18
-
-
Save ethaizone/6908a48f042e4dfea7cca6d49a7d3c86 to your computer and use it in GitHub Desktop.
Helper function for create blank array by number of each dimension.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Create blank array by number | |
* | |
* @param int Number of members in each dimension | |
* @return array | |
*/ | |
function createBlankArray() | |
{ | |
$dimensions = func_get_args(); | |
$base = null; | |
while ($amount = array_shift($dimensions)) { | |
$callback = function(&$item) use ($amount) { | |
$item = []; | |
for ($i = 0; $i < $amount; $i++) { | |
$item[] = null; | |
} | |
}; | |
if (is_array($base)) { | |
array_walk_recursive($base, $callback); | |
} else { | |
$callback($base); | |
} | |
} | |
return $base; | |
} | |
/** | |
* Usage: | |
* $output = createBlankArray(3, 1, 4, 2); | |
* | |
* Output: | |
* array:3 [▼ | |
* 0 => array:1 [▼ | |
* 0 => array:4 [▼ | |
* 0 => array:2 [▼ | |
* 0 => null | |
* 1 => null | |
* ] | |
* 1 => array:2 [▼ | |
* 0 => null | |
* 1 => null | |
* ] | |
* 2 => array:2 [▶] | |
* 3 => array:2 [▶] | |
* ] | |
* ] | |
* 1 => array:1 [▶] | |
* 2 => array:1 [▶] | |
* ] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment