Rusty3 Posted September 4, 2009 Share Posted September 4, 2009 Now I found this code snippet somewhere (I think it was on php.net) and have used it on my scripts, however, today I have found it is buggy!!! This code autopads an array: function autopad($array=array(), $length=0){ $length=abs((int)$length); if($length<=sizeof($array)){return $array;}; $originalSize=sizeof($array); while(sizeof($array)<$length){ $addQuantity=$length - sizeof($array); if($addQuantity<$originalSize){$originalSize=$addQuantity;}; $array=array_merge($array, array_slice($array, 0, $originalSize)); } return $array; } Now, this works fine: $arr = array("one","two","three","one","two","three","one","two","three","one","two"); $arr = autopad($arr, 30000); But this doesn't: $arr = array("one","two","three"); $arr = autopad($arr, 30000); Isn't that a mistery or what?! Quote Link to comment https://forums.phpfreaks.com/topic/173161-solved-autopad-an-array/ Share on other sites More sharing options...
Rusty3 Posted September 4, 2009 Author Share Posted September 4, 2009 Found the source: (I think) http://www.fullposter.com/snippets.php?snippet=40 Quote Link to comment https://forums.phpfreaks.com/topic/173161-solved-autopad-an-array/#findComment-912709 Share on other sites More sharing options...
Psycho Posted September 4, 2009 Share Posted September 4, 2009 No reason that shouldn't work, but that function is way more overly complicated than it needs to be. This will do the same thing much more efficiently: function autopad($array=array(), $length=0) { $outputSize = abs((int)$length); while($outputSize>sizeof($array)) { $arrSize = sizeof($array); $array = array_merge($array, array_slice($array, 0, Min(($outputSize-$arrSize), $arrSize))); } return $array; } After further review, the original code is even more flawed than I thought. In addition to the inefficient logic, the orignal code would continue merging the original array into the output array until the length was met. So, if the original array was 10 elements long and you needed it to be 100 elements long, it would take 9 loops to get to that length. The code I posted will add the "current" array to itself until it reaches thecorrect length. Therefore on each cycle of the loop, the current array is twice the size it was on the last cycle. To get to 100 records a 10 record array would need only 4 cycles of the loop: Original: 10 First cycle: 20 (10 + 10) Second cycle 40 (20 + 20) Third cycle 80 (40 + 40) Fourth cycle 100 (80 + 20) As you can see the reduction in processing will be exponential the larger you need the output to be. Using the larger of your sample arrays above with an output size of 3000, the original function required 272 loops, wheras the second function only required 9. Quote Link to comment https://forums.phpfreaks.com/topic/173161-solved-autopad-an-array/#findComment-912714 Share on other sites More sharing options...
Rusty3 Posted September 4, 2009 Author Share Posted September 4, 2009 wow, you're right. Your function is brazzing fast!!! Thanks, thanks. Where's the Thank You / rep / anything button? Quote Link to comment https://forums.phpfreaks.com/topic/173161-solved-autopad-an-array/#findComment-912751 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.