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

1
  • 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. Commented Jan 24, 2011 at 6:13

5 Answers 5

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

$yourArray = array_map( 'multiplyElements' , $yourArray );
Sign up to request clarification or add additional context in comments.

2 Comments

Though there shouldn't be any performance gain - this answer at-least sticks to the point and answers what the OP asked :)
@JP19 Though it is with iteration, just implicit.
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.

Comments

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().

3 Comments

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.
@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.
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'
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.

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.