Enjoy an ad free experience by logging in. Not a member yet?
Register .
05-18-2012, 03:02 PM
PM User |
#1
New Coder
Join Date: Sep 2011
Posts: 99
Thanks: 0
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( 1000 , 2000 , 2500 , 1500 );
$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
05-18-2012, 03:20 PM
PM User |
#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( 1000 , 2000 , 2500 , 1500 );
$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 ..
05-18-2012, 03:20 PM
PM User |
#3
Senior Coder
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,658
Thanks: 45
Thanked 456 Times in 444 Posts
PHP Code:
<?php function multiply (& $Value , $Index , $By ) //Note & character - means passed by reference { $Value = ( $Value * $By ); } $array = array( 1000 , 2000 , 2500 , 1500 ); array_walk ( $array , 'multiply' , 1.5 ); var_dump ( $array ); ?>
You can see it in action
here .
05-18-2012, 03:24 PM
PM User |
#4
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
No, you can also use an array_walk:
PHP Code:
function multiplyByX (& $val , $key , $dMult ) { $val *= $dMult ; } $array = array( 1000 , 2000 , 2500 , 1500 ); 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
05-18-2012, 03:27 PM
PM User |
#5
Senior Coder
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,658
Thanks: 45
Thanked 456 Times in 444 Posts
Quote:
Originally Posted by
Fou-Lu
Edit:
Nooo too slow this time >.<
lol
Muhahaha
05-18-2012, 03:31 PM
PM User |
#6
Supreme Master coder!
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
PHP Code:
<?php $array = array( 1000 , 2000 , 2500 , 1500 ); 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)
05-18-2012, 03:37 PM
PM User |
#7
New Coder
Join Date: Sep 2011
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
Hmm several methods to use, I'll look which one is best in performance. Thank you all.
Jump To Top of Thread
Thread Tools
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
HTML code is Off
All times are GMT +1. The time now is 06:19 AM .
Advertisement
Log in to turn off these ads.