0

I have arrays where keys is math operations to it's values.

  $a = array('1'=>'100','4'=>'25','3'=>'10');
  $b = array('1'=>'120','5'=>'40','3'=>'5');
  $c = array('1'=>'300','3'=>'15','2'=>'75');
  $d = array('1'=>'75','5'=>'15');

The list of operations:

   $list = array(
            '1' => 'Set the price',
            '2' => 'Raise by the number',
            '3' => 'Decrease by the number',
            '4' => 'Increase by %',
            '5' => 'Decrease by %',
        );

So i need to get the single value from each array and this value will be the result of math operations e.g:

1) $summ_a =  100 + 100*0,25  -10;
2) $summ_b =  120 - 120*0,4   -5;
3) $summ_c =  300 - 15        +75;

I tried ineffectively built-in php functions like array_walk, array_reduce, etc and have no idea how to achieve this.

How to achieve this? Any help would by greatly appreciate.

NorteAmaru
  • 11
  • 1
  • 5
  • 3
    Show what you've tried. I probably wouldn't use this structure as you're limited to one operation of each type since keys are unique. – Devon Bessemer Jun 15 '18 at 14:24
  • array_reduce and a switch statement for the operation sounds like the right approach, but as said already, this is a bit of a weird data structure – iainn Jun 15 '18 at 14:58
  • just curious on how you get -30? – Mawia HL Jun 15 '18 at 15:42
  • I think you'd be better off with using the operators in your operations array. Then you could use something like what is explained in the following: https://stackoverflow.com/a/16071456/1594754 – Bryan Jun 15 '18 at 22:30

0 Answers0