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
    118
    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. #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. #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,821
    Thanks
    79
    Thanked 575 Times in 562 Posts
    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.
    Quote Originally Posted by deathshadow View Post
    So seriously, loosen up that tie, let out the belt, and try relating to normal people on the street instead of the gentleman's club crowd.

  4. #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    17,027
    Thanks
    4
    Thanked 2,668 Times in 2,637 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. #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,821
    Thanks
    79
    Thanked 575 Times in 562 Posts
    Quote Originally Posted by Fou-Lu View Post
    Edit:
    Nooo too slow this time >.<
    lol
    Muhahaha
    Quote Originally Posted by deathshadow View Post
    So seriously, loosen up that tie, let out the belt, and try relating to normal people on the street instead of the gentleman's club crowd.

  6. #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    15,023
    Thanks
    164
    Thanked 2,247 Times in 2,234 Posts
    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. #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    118
    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
  •