XCode2015 XCode2015 - 4 months ago 14
PHP Question

Handle number in string php (Part2)

this is my problem 1 here: Handle number in string PHP . I sloved it.

Now, i see new problem, you can see the picture:

enter image description here

I want to get only number, not date (500000 and 200000) and sum it.

This is my code without date:

$total= 0;
$ex = explode(' ',$_POST['txtSalary']);
function total($ex) {
global $total;
return $total+=$ex;
}
array_map('total',$ex);
echo $total."<br/>";


I try so much but no result, hope you can help me. Thank you!

Answer

I assume that your $_POST['txtSalary'] look like below:-

$_POST['txtSalary'] = '-27/07/2016: 5000000
-01/08/2016: 2000000';

So do like below:-

<?php

$_POST['txtSalary'] = '-27/07/2016: 5000000
-01/08/2016: 2000000';

$array = explode(PHP_EOL, $_POST['txtSalary']);
print_r($array);

$sum = 0;
foreach($array as $arr){
   $sum += explode(': ',$arr)[1];
}

echo $sum;

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

Comments