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 05-18-2012, 03:02 PM   PM User | #1
Krentenbol
New Coder

 
Join Date: Sep 2011
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
Krentenbol is an unknown quantity at this point
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:
Quote:
Fatal error: Unsupported operand types
Krentenbol is offline   Reply With Quote
Old 05-18-2012, 03:20 PM   PM User | #2
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
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..
jmj001 is offline   Reply With Quote
Old 05-18-2012, 03:20 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,658
Thanks: 45
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 05-18-2012, 03:24 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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
Fou-Lu is offline   Reply With Quote
Old 05-18-2012, 03:27 PM   PM User | #5
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,658
Thanks: 45
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by Fou-Lu View Post
Edit:
Nooo too slow this time >.<
lol
Muhahaha
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 05-18-2012, 03:31 PM   PM User | #6
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
PHP Code:
<?php
$array 
= array(1000200025001500); 
function 
multiply($elm){
    return 
$elm 2;
}
$array=array_map('multiply',$array);
print_r($array);
?>
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 05-18-2012, 03:37 PM   PM User | #7
Krentenbol
New Coder

 
Join Date: Sep 2011
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
Krentenbol is an unknown quantity at this point
Hmm several methods to use, I'll look which one is best in performance. Thank you all.
Krentenbol 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 06:19 AM.


Advertisement
Log in to turn off these ads.