Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-05-2007, 09:47 PM   PM User | #1
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
Using ROUND() on Array Elements

Hi All,

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.

Any ideas?
Many thanks for your times.

Sir P
sir pannels is offline   Reply With Quote
Old 02-05-2007, 10:00 PM   PM User | #2
anarchy3200
Regular Coder

 
anarchy3200's Avatar
 
Join Date: Mar 2003
Location: England
Posts: 261
Thanks: 0
Thanked 1 Time in 1 Post
anarchy3200 is an unknown quantity at this point
How about going through them in a foreach:

PHP Code:
$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
anarchy3200 is offline   Reply With Quote
Old 02-05-2007, 10:04 PM   PM User | #3
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,549
Thanks: 15
Thanked 131 Times in 124 Posts
chump2877 is on a distinguished road
Are you sure your array elements are considered numbers?

i.e., I don;t know for sure if these 2 declarations are interpreted the same way:
PHP Code:
$array[1] = 1.5;
$array[1] = '1.5'
__________________
Regards, R.J.

---------------------------------------------------------

Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
[Related videos and tutorials are also available at my YouTube channel]

Last edited by chump2877; 02-05-2007 at 10:17 PM..
chump2877 is offline   Reply With Quote
Old 02-05-2007, 10:12 PM   PM User | #4
anarchy3200
Regular Coder

 
anarchy3200's Avatar
 
Join Date: Mar 2003
Location: England
Posts: 261
Thanks: 0
Thanked 1 Time in 1 Post
anarchy3200 is an unknown quantity at this point
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!
__________________
Mike
anarchy3200 is offline   Reply With Quote
Old 02-05-2007, 10:16 PM   PM User | #5
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
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.

Thanks again for you time both of you.
Regards,
sir pannels is offline   Reply With Quote
Old 02-05-2007, 10:20 PM   PM User | #6
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
The usual idiom for applying and function to an array and substituting its values with the result is map, although it's kind of clunky in PHP:

PHP Code:
$ints array_map('round'$floats); 
ralph l mayo is offline   Reply With Quote
Old 02-05-2007, 10:22 PM   PM User | #7
anarchy3200
Regular Coder

 
anarchy3200's Avatar
 
Join Date: Mar 2003
Location: England
Posts: 261
Thanks: 0
Thanked 1 Time in 1 Post
anarchy3200 is an unknown quantity at this point
Ok, so after you try ralph l mayo's idea, whats the outcome if you try using a different function like ceil() or floor(), same 0 output?
__________________
Mike
anarchy3200 is offline   Reply With Quote
Old 02-05-2007, 10:59 PM   PM User | #8
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
Ok anarchy, let me just try ralphs then...

Ralph, tried it but maybe im misunderstanding the syntax, i have not used array_map before, would I be using ..

PHP Code:
$array array_map('round'$array); 
? I get errors that the second resource should be an array. Is that reffering to the word 'round', is that the bit I misunderstood?

Thanks for all your time everyone
sir pannels is offline   Reply With Quote
Old 02-06-2007, 12:05 AM   PM User | #9
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,549
Thanks: 15
Thanked 131 Times in 124 Posts
chump2877 is on a distinguished road
Can you do:

PHP Code:
var_dump($array); 
And post the output here in this thread...

Something doesn;t add up here...I don;t see why you are having this problem...

Once I see the output, I can create my own array with the same key-value pairs, and recreate your situation...And see if I get the same results
__________________
Regards, R.J.

---------------------------------------------------------

Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
[Related videos and tutorials are also available at my YouTube channel]
chump2877 is offline   Reply With Quote
Old 02-06-2007, 10:31 AM   PM User | #10
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
Ok Chump great thanks...

If I do not do any rounding and var dump the array I get the following.
Quote:
array(10) { [0]=> float(18.8583358187) [1]=> float(13.8956158664) [2]=> float(752.348344766) [3]=> int(0) [4]=> int(0) [5]=> float(129.030718759) [6]=> int(0) [7]=> int(0) [8]=> int(0) [9]=> int(0) }
If I try and round using
PHP Code:
$array[$i] = round($array[$i]); 
And then var dump I get the following.
Quote:
array(10) { [0]=> float(0) [1]=> float(0) [2]=> float(0) [3]=> float(0) [4]=> float(0) [5]=> float(0) [6]=> float(0) [7]=> float(0) [8]=> float(0) [9]=> float(0) }

Thanks for all your time

Last edited by sir pannels; 02-06-2007 at 10:52 AM..
sir pannels is offline   Reply With Quote
Old 02-06-2007, 11:01 AM   PM User | #11
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
I have resolved it!!

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.

All the best,
Sir P
sir pannels is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:58 AM.


Advertisement
Log in to turn off these ads.