0

I'm pulling values from two different arrays to produce new arrays with only the values I want. Here are the two scripts. I'm basically finding the total revenue for all teams within a game.

Script 1

$allSales = array();
foreach ($game['sales'] as $sale) {
  foreach ($sale['values'] as $id => $values) {
    $allSales[$id]+=$values['y'];
  }
}  

print_r($allSales);

Script 2

$allPrices = array();
foreach ($game['products'] as $id => $product) {
  $allPrices[$id]+=$product['Price'];
}    

print_r($allPrices);

The values I get look something like this:

Array ( [0] => 500000000 [1] => 300000000 ) 
Array ( [0] => 300 [1] => 600 )

I want to be able to multiply the one array against the other in the same order of position. So 500000000 * 300 and 300000000 * 600 and produce a new array.

The reason I want a new array is because I will be sorting them from largest to smallest value and adding ranks to all of the values. I have another variable that has determined the current teams total sales revenue which I will compare to find their rank.

3 Answers 3

2

maybe something along these lines:

$result = array_map(function($a,$b){ return $a*$b; }, $array1, $array2);

see http://php.net/manual/en/function.array-map.php

Proof:

~$ php -a
Interactive shell

php > $result = array_map(function($a,$b){ return $a*$b; }, array(1,2), array(3,4));
php > print_r($result);
Array
(
    [0] => 3
    [1] => 8
)
php > 
Sign up to request clarification or add additional context in comments.

5 Comments

Oh I didn't see you basically did a copy paste of a basic MAP, I thought you had actually written in the array names so I just copied and pasted. Sorry bit on the tired side.
I personally find this version better, but it would be even better when combined in it's own function.
@Lumio; you mean like $array_mul = function($a,$b)( return "what I wrote"; ) - why would that be better ?
No, I mean like function combineValues($a, $b) { return 'what you did with the right indentation etc'; } - and why? I personally find it more readable that having it as a oneliner
well yes, I agree if you use it more than once - but the only "better" thing you get out of it is that a function a(.. is compile-time optimized by php. A far as "code style" goes I would not mix a 'pure' function return map(fn,a,b) with an outputting one print. reduces re-usability
1

When both arrays have the same length then you could do

$comparison = array();
foreach ($allPrices as $index => $value) {
  $comparison[$index] = $value * $allSales[$index];
}

print_r($comparison);

Comments

1
$finalArray = array();
foreach ($allSales as $id => $eachSale) {
    $finalArray[$id]=$eachSale * $allPrices[$id];
} 
print_r($finalArray );

3 Comments

Tried this one and it only produced the sale price for each. Very close. @lumio's work though, should compare the difference.
@dvoutt: I couldnt understand what do u mean?
I think it might of been the space in the print out. Either way the two scripts are the same so it should work just didn't on first attempt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.