Jump to content

[SOLVED] Autopad an array


Rusty3

Recommended Posts

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?!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.