-3

this is my code. for example.

<?php 
  $testing="70, 60, 60, 30";
  $test=explode(",",$testing);
  $total=$test[1]+$test[2]+$test[3];
  echo $total-$test[0];
?> 

Now i want that type of function which auto add the all last keys of array except first key of that array. and finally the total of all last keys of array is subtract from first key of array. Anyone know how i do that.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Vishal
  • 109
  • 1
  • 4
  • 11
  • You're question is unclear, "auto add the all last keys of array except first key of that array. and finally the total of all last keys of array is subtract from first key of array" - What? – Epodax Jun 27 '17 at 08:51
  • what you want extactly @Vishal?? – Harsh Panchal Jun 27 '17 at 08:52
  • hope this [link](https://stackoverflow.com/questions/1496682/how-to-sum-values-of-the-array-of-the-same-key) helps you ..just subtract the last key value. – Ketan Solanki Jun 27 '17 at 08:53
  • means brother i want addition of last keys of array and then when i get it,,, I want difference between first key and addition of all last keys. for e.g my first key is 70 and my all last keys is 60+60+30=150. Now total of last keys is 150. and finally i want 150-70=80. I hope now it will clear – Vishal Jun 27 '17 at 08:54

4 Answers4

1

Do it like below:-

<?php 
$testing="70, 60, 60, 30";
$test=explode(",",$testing);

function array_manipulation($array){
  $final_val = 0;
  for($i=1;$i<=(count($array)-1);$i++){
  $final_val += $array[$i];

  }
  return $final_val-$array[0];
}
echo array_manipulation($test);
?> 

Output:- https://eval.in/822897

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
1
<?php 
$total=0;
$testing="70, 60, 60, 30";
$test=explode(",",$testing);
for($i=0;$i<count($test);$i++){
if($i==0){}
else{$total +=$test[$i];}
}
echo $total-$test[0];
?> 
Osama
  • 2,912
  • 1
  • 12
  • 15
1

There are many ways to accomplish such a simple goal using a single line of code (well, two lines sometimes) that works with any number of values in the input array:

Solution #1

// Extract the first value from the array
$first = array_shift($test);
// Subtract the first value from the sum of the rest
echo(array_sum($test) - $first);

Solution #2

// Change the sign of the first value in the array (turn addition to subtraction)
$test[0] = -$test[0];
// Add all values; the first one will be subtracted because we  changed its sign
echo(array_sum($test));

Solution #3

// Sum all values, subtract the first one twice to compensate
echo(array_sum($test) - 2 * $test[0]));
// There is no second line; that's all

Solution #4

// Compute the sum of all but the first value, subtract the first value
echo(array_sum(array_chunk($test, 1)) - $test[0]);
// There is no second line; two function calls are already too much

Solution #5

Left as an exercise to the reader.
Useful reading: the documentation of PHP array functions.

Community
  • 1
  • 1
axiac
  • 68,258
  • 9
  • 99
  • 134
  • Nice comprehensive catalog of solutions, I would not chose the first two ones as the original array gets modified, though. – Eineki Jun 27 '17 at 09:17
  • There is no "original array" in the question. It is generated by `explode()`-ing an "original" string. However, as a general remark, solution #3 is the best. It is the fastest (only one function call) and it doesn't modify the array. – axiac Jun 27 '17 at 09:20
0

Hope this helps.

   <?php 
     $testing="70, 60, 60, 30";
     $test=explode(",",$testing);
     $total=0;
     foreach ($test as $key =>$value) {
       if($key === 0) {
           $total = $total - $value;
       } else  {
          $total = $total + $value;
       }
      }
    echo $total;
    ?> 
mpsbhat
  • 2,733
  • 12
  • 49
  • 105