Omirion Posted October 5, 2010 Share Posted October 5, 2010 I'll try to explain as best i can. I'm trying to write a function that creates n-dimensional arrays. Why i need this is far to long to explain. But imagine you have this. function createArr($depth,$key,$val){ } And you call it like so. $a = createArr(3,0,'moo') The result would be $a[0][0][0] == 'moo' Say you call it like this. $a = createArr('7','15','foo'); You would get $a[0][0][0][0][0][0][15] = 'foo' Any ideas on how this effect can be achieved? Basically i need to add as many []'s as i specify in the funcs depth argument. Quote Link to comment Share on other sites More sharing options...
Octo Posted October 5, 2010 Share Posted October 5, 2010 function createArr($depth,$key,$val){ $end = array($val); // so $end[0] = $val; $end = array_pad($end, $key*-1, 0); // adds $key number of 0s in front of the $val into the array making $end[$key] = $val; for ($i = 0; $i < $depth - 1; $i++) { $end = array($end); // stacks the array into another array $depth - 1 times; } return $end; } Something like that? Quote Link to comment Share on other sites More sharing options...
Omirion Posted October 7, 2010 Author Share Posted October 7, 2010 You do know you're my new personal god and master right? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.