Just a quick one I hope. I have an array with 10 elements and each element as a numerical value. I want to use ROUND to round each value to 0 decimal places and have tried the following:
PHP Code:
$element[1] = round($array[1]);
However that did not work, so I tried rounding the entire array at once using:
PHP Code:
$newarray[] = round($array[]);
No work either. No errors, they just set the newarray to 0, no matter what the values before.
$element = array(1,2,1.5,2.2,5.9); //Test Array
print_r($element); //Show Before
foreach(array_keys($element) AS $ek){ //Get key of each value so it can be set back to itself
$element[$ek] = round($element[$ek]); //Do rounding
}
print_r($element); //Show array afterwards
Just thinking about it your first example should have worked really so not sure what went wrong there... my snippet at least works for me :P
__________________ Mike
Last edited by anarchy3200; 02-05-2007 at 10:03 PM..
Reason: Addition
Chump2877: just tested your idea as its a fair assumption but round() still actually works even if the numbers are set with quotes or even typecast as a string, appears to be quite forgiving!
Hi anarchy3200 , thanks for your snipplet, yeh yours is pretty much same as mine and has the same effect, everything gets changed to zero. D'oh. Cheers for you time though.
Hey Chump, I put your code in to debug and damn, every value was indeed a number.. which brings me no closer to solving. Dang.
Not actually quite sure how, though I believe it was a typo somewhere. I just wrote it again using the $array[$i] = round($array[$i]); approach in a whileloop and it has worked great, correct results now.
I looked back at the other code and still cannot see a typo, but there must be.
When I staretd this and posted yesterday it was about midnight after a long day hha.
I would like to thank you all for your kind help and support.