0

I have an array consisting of more than 25k elements. All array values are integer. NOw i want to multiply all elements of this array by some number "n".... how can I do that with out iterating thru each element using a i.e. without using a foreach?

I am looking for this bcoz iterating thru such large array might affect the performance ...

Deepak

ʞɹᴉʞ ǝʌɐp
  • 5,350
  • 8
  • 39
  • 65
  • 8
    You are going to have to iterate over the array somehow. Even something like `array_map()` (see answers using it below) causes PHP to iterate over the array internally. – BoltClock Jan 24 '11 at 06:13

5 Answers5

1
function multiplyElements( $inElem ){
  return $inElem * n; // Where "n" is the number to multiply all elements by
}

$yourArray = array_map( 'multiplyElements' , $yourArray );
Luke Stevenson
  • 10,357
  • 2
  • 26
  • 41
  • Though there shouldn't be any performance gain - this answer at-least sticks to the point and answers what the OP asked :) –  Jan 24 '11 at 06:25
  • @JP19 Though it *is* with iteration, just implicit. – alex Jan 24 '11 at 06:26
1

"might affect the performance" is not a valid reason to optimize. Make sure it does affect performance. If you are sure it negatively affects performance, consider using one of the Spl Data Structures

This would still leave you iterating, but for large datasets, those datastructures can make a difference in both execution speed and memory consumption.

Gordon
  • 312,688
  • 75
  • 539
  • 559
0

Use the Nested function like below

function arrayFunc($e){
    if(is_array($e)) return array_map('arrayFunc', $e);
    else return $e; // Set your changes here for example change this line to : else return $e * 1000;
}
$array = arrayFunc($array);
Behnam Bakhshi
  • 263
  • 2
  • 6
0

There is an array_sum(), but not array multiplier function.

You will need to iterate (or use array_map(), which just abstracts the iteration from you).

I would also make sure your number doesn't overflow PHP's max integer size (check PHP_INT_MAX on your platform). You may need to use gcmul().

alex
  • 479,566
  • 201
  • 878
  • 984
  • Just my two cents: Given that the question itself is slightly silly, and I don't understand what value the "overflow" comment add - thats true for any multiplication operation in PHP and is nothing specific to this question. –  Jan 24 '11 at 06:27
  • 1
    @JP19 I read it as `How to multiply all array elements [together]`, which he noted there were 25k - that *could* indeed become a large number. – alex Jan 24 '11 at 08:07
  • Oh sorry. Then my comment is really not valid and your answer is indeed useful. I doubt he meant multiple 25K numbers together though, especially since he talks about 'n' –  Jan 24 '11 at 08:10
0

Well because of bunch of deleted answers, which were right ones.

  1. You are going to have to iterate over the array somehow. Even using something like array_map() (see other two answers) causes PHP to iterate over the array internally.

  2. The best way to optimize 25k iterations is to avoid such large numbers at all. Or at least place it in the background where performance wouldn't be significant.

  3. If you came to that point anyway, the fastest way is a matter of test. it seems aaray_map is worst one.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345