Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts

    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

  • #2
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    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
    Last edited by anarchy3200; 02-05-2007 at 10:03 PM. Reason: Addition
    Mike

  • #3
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,798
    Thanks
    19
    Thanked 156 Times in 147 Posts
    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'
    Last edited by chump2877; 02-05-2007 at 10:17 PM.
    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 and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #4
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    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

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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,

  • #6
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    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); 

  • #7
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    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

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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

  • #9
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,798
    Thanks
    19
    Thanked 156 Times in 147 Posts
    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 and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Ok Chump great thanks...

    If I do not do any rounding and var dump the array I get the following.
    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.
    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

  • #11
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •