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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Multiply an entire array by some value

    I want to multiply an array by some value, in this case it's 1.5. I wrote this:

    PHP Code:
    <?php
    $array 
    = array(1000200025001500);
    $i 0;
    while (
    $i 4) {
        
    $array[$i] = $array[$i] * 1.5;
        echo 
    "$array[$i] <br />";
        
    $i++;
    }
    ?>
    Is this the easiest way or is there some function which allows me to just multiply $array with some value. Since if I try this:

    PHP Code:
    $array $array 1.5
    It throws out this error:
    Fatal error: Unsupported operand types

  • #2
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    afaik the loop is the easiest way

    you could easily create your own function with that loop

    PHP Code:
    <?php


    $array 
    = array(1000200025001500);
    $newArray inc_my_array_vals($array,1.5);


    function 
    inc_my_array_vals($arr,$incVal){
        
    $newArr = array();
         
    $j count($arr);
         for(
    $i 0$i $j$i++){
            
    $newArr[$i] = $arr[$i] * incVal;
          }

         return 
    $newArr;
    }
    ?>
    Last edited by jmj001; 05-18-2012 at 03:27 PM.

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,301
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    PHP Code:
    <?php
    function multiply(&$Value$Index$By//Note & character - means passed by reference
       
    {
       
    $Value = ($Value $By);
       }

    $array = array(1000200025001500); 

    array_walk($array'multiply'1.5);

    var_dump($array);
    ?>
    You can see it in action here.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No, you can also use an array_walk:
    PHP Code:
    function multiplyByX(&$val$key$dMult)
    {
        
    $val *= $dMult;
    }

    $array = array(1000200025001500); 
    array_walk($array'multiplyByX'1.5);
    print_r($array); 
    Not much different code wise between writing a function to multiply and writing a function for the walk. Only thing you don't need to do is loop since that's handled under the hood instead.

    Edit:
    Nooo too slow this time >.<
    lol

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,301
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by Fou-Lu View Post
    Edit:
    Nooo too slow this time >.<
    lol
    Muhahaha
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    <?php
    $array 
    = array(1000200025001500); 
    function 
    multiply($elm){
        return 
    $elm 2;
    }
    $array=array_map('multiply',$array);
    print_r($array);
    ?>
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hmm several methods to use, I'll look which one is best in performance. Thank you all.


  •  

    Posting Permissions

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