Array functions not working the way I had expected.

Hi all,

I am trying to create a function that will generate and return an array of shoe sizes. So far I have two loops that are required to create the sizes due to the fact that the sizes are 1/2 sizes from 6 - 11.5 and full sizes from 12 to 20. The code I have nearly works until I tried to put it in a function and get the two arrays to merge at the end. I think the issue is with array_merge thinking the keys are part of an indexed array because they are numbers. I will stop rambling and post what I have so far.
[php]function generate_sizes($half_start, $half_stop, $full_start, $full_stop) {
for($i=$half_start; $i<=$half_stop; $i=$i+.5) {
$first[] = $i;
}
$first_form = str_replace(".", “_”, $first);
$first = array_combine($first, $first_form);

	for($i = $full_start; $i <= $full_stop; $i++) {
		$second[] = $i;
	}
	$second = array_combine($second, $second);
	
	$size = array_merge($first, $second);

	return $size;
}[/php]

P.S. the str_replace part in the first array is to avoid having an html input named 7.5. name will change to 7_5 instead.

Why not just one loop???
[php]
function generate_sizes($start, $stop) {
for($i=$start; $i<=$stop; $i=$i+.5) {
if($i != ($i % 1)) // If ( remainder of $i divided by 1 ) not zero…
$i=$i + .5;
$size[] = str_replace(".", “_”, $i);
}
return $size;
}
[/php]
Something like that should work… Just code, not tested for you…

Thanks for the reply but that just creates 6_5 - 20_5 in half steps when I put in the numbers. looking to input 6 as a starting and 11.5 as the end of the half sizes and continue with 12 - 20 as whole sizes. i.e. 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 13, 14, 15, 16, 17, 18, 19, 20. I don’t see how the one loop will do that. thanks for your patience.

Yes, this works!

[php]
for($i=$start-1; $i<=$stop; $i=$i+.5) {
if(($i > 11) || ($i % 1 > 0)) // If ( remainder of $i divided by 1 ) not zero…
$i=$i + .5;
$size[] = str_replace(".", “_”, $i);
}
[/php]
I forgot about the 12 and above being whole numbers and my module was off a little…
Should be all set now…

Thanks, that will work fine.

You are welcome… CYA in the bitstream…

Sponsor our Newsletter | Privacy Policy | Terms of Service