I've got field in my database which contain strings like 21;48;33;22;31. Then I'd like to convert it to mathematical calculation 21+48+33+22+31.
$points = "21;48;33;22;31";
$points = str_replace(";","+",$points );
$points = preg_replace('/[^0-9\+\-\*\/\s]+/', '', $points);
echo $points;
But that simply does not work. I've got the same string "21+48+33+22+31" instead of the sum.
$points = "21;48;33;22;31";
$points = explode(';',$points );
$points = array_sum($points);
echo $points;
$points = "21;48;33;22;31";
$arr = explode(";", $points);
$points = 0;
foreach($arr as $key => $rows){
$points += $rows[$key];
}
echo $points;
Try above code it will give you proper result.
or you can try it also:
$points = "21;48;33;22;31";
$arr = explode(";", $points);
echo $points = array_sum($arr);
The easiest way is to explode the string.
Then you can iterate with foreach over the resulting array and calculate them.
$points = "21;48;33;22;31";
$points = explode(";", $points);
$calc = 0;
forearch($points as $point) {
$calc += $point;
}
Or your can use array_sum:
$points = "21;48;33;22;31";
$points = explode(";", $points);
$calc = array_sum($points);
Something like that. Perhaps there are some shorter ways.
$string = "21;48;33;22;31";
$string = explode(";" , "21;48;33;22;31");
$point = 0;
foreach($string as $num)
{ // by using (int) you can convert string to int.
$point = (int)$num + $point;
}
print($point);
// output
155
explode it then loop for sum...
<?php
$total = 0; // for getting the total
$points = "21;48;33;22;31";
$points = explode(';',$points);
for($i=0;$i<count($points);$i++){
$total+= $points[$i];
}
echo $total;
?>
<?php
eval('$sum = ' . str_replace(';','+','21;48;33;22;31').';');
echo $sum;
Related
Hey i have array calculation where i need to refactor this code using loops please suggest...
<?php
arr = [286538,237034,192724,150815,117523,82754,49707];
$a1 = $arr[0]/7;
$a2 = ($a1+$arr[1])/7;
$a3 = ($a1+$a2+$arr[2])/7;
$a4 = ($a1+$a2+$a3+$arr[3])/7;
$a5 = ($a1+$a2+$a3+$a4+$arr[4])/7;
$a6 = ($a1+$a2+$a3+$a4+$a5+$arr[5])/7;
$a7 = ($a1+$a2+$a3+$a4+$a5+$a6+$arr[6])/7;
?>
Output -
40934
39709.714285714
39052.530612245
38644.463556851
39409.10120783
40071.972808949
41075.540353084
Here is how you can utilize loops in your problem:
$arr = [286538,237034,192724,150815,117523,82754,49707];
$newArr = [];
$counter = 0;
while($counter < count($arr)){
$sum = array_sum($newArr);
$newArr[] = ($sum + $arr[$counter]) / 7;
$counter++;
}
<?php
$arr = [286538,237034,192724,150815,117523,82754,49707];
function calcarr($n){
$result = 0;
global $arr;
if ($n <= 0) return $result;
for ($i=0;$i<$n;$i++) $result += calcarr($i);
return ($result + $arr[$n-1])/7;
}
$a1 = $arr[0]/7;
$a2 = ($a1+$arr[1])/7;
$a3 = ($a1+$a2+$arr[2])/7;
$a4 = ($a1+$a2+$a3+$arr[3])/7;
$a5 = ($a1+$a2+$a3+$a4+$arr[4])/7;
$a6 = ($a1+$a2+$a3+$a4+$a5+$arr[5])/7;
$a7 = ($a1+$a2+$a3+$a4+$a5+$a6+$arr[6])/7;
echo "$a7\n";
echo calcarr(7)."\n";
?>
RESULT:
41075.540353084
41075.540353084
<?php
$arr = [286538,237034,192724,150815,117523,82754,49707];
$result = [];
foreach($arr as $k => $item)
$result["a$k"] = (float) (array_sum($result) + $item)/7;
var_dump($result);
Output:
array(7) {
["a0"]=>
float(40934)
["a1"]=>
float(39709.714285714)
["a2"]=>
float(39052.530612245)
["a3"]=>
float(38644.463556851)
["a4"]=>
float(39409.10120783)
["a5"]=>
float(40071.972808949)
["a6"]=>
float(41075.540353084)
}
If you want the variables as you have listed:
extract($result);
If you don't care about keys and to extract those variables, this will just give you a 0 indexed array as for your result:
foreach($arr as $item)
$result[] = (float) (array_sum($result) + $item)/7;
I have problem assigning values to the $word as array so that I can make it as a variable.
I am displaying an output of
1,000,000,000 they have all different echos; I want to make them as 1 value only example:
$output = $arramt[1]="1",$arramt[2]="000",$arramt[3]="000",$arramt[4]="000"
function mnyFmt($word){
$n = strlen($word);
if ($n%3==0){
$i = $n/3;
$x=0;
while($x<$i-1){
echo substr($word,-$n,3).",";
$arramt[$x] = substr($word,-$n,3).",";
$x++;
$n = $n-3;
}
echo substr($word,-$n,3);
$arramt[$x] = substr($word,-$n,3).",";
}
elseif($n%3==1){
$word1 = substr($word,1,$n);
$w1 = strlen($word1);
$i = $w1/3;
echo substr($word,0,1).",";
$x=0;
while($x<$i-1){
echo substr($word1,-$w1,3).",";
$arramt[$x] = substr($word1,-$w1,3).",";
$x++;
$w1 = $w1-3;
}
echo substr($word1,-$w1,3);
$arramt[$x] = substr($word1,-$w1,3).",";
echo array_values($arramt[$x]);
}
elseif($n%3==2){
$word1 = substr($word,2,$n);
$w1 = strlen($word1);
$i = $w1/3;
echo substr($word,0,2).",";
$x=0;
while($x<$i-1){
echo substr($word1,-$w1,3).",";
$x++;
$w1 = $w1-3;
}
echo substr($word1,-$w1,3);
}
}
$amount = 1000000000;
mnyFmt($amount);
}
Thanks very much in advance.
You don't need to make any of the custom function you can simply use number_format and explode function like as
$amount = 1000000000;
print_r(explode(',',number_format($amount)));
Demo
Let's say I have this array
$number = [2,1,4,3,6,2];
First pair the elements on an array by two's and find their difference
so this is the output in the first requirement
$diff[] = [1,1,4];
Second sum all the difference
this is the final output
$sum[] = [6];
Conditions:
the array size is always even
the first element in a pair is always greater than the second one, so their is no negative difference
What I've done so far is just counting the size of an array then after that I don't know how to pair them by two's. T_T
Is this possible in php? Is there a built in function to do it?
One line:
$number = [2,1,4,3,6,2];
$total = array_sum(array_map(function ($array) {
return current($array) - next($array);
}, array_chunk($number, 2)));
echo $total;
This should work fine:
<?
$number = array(2,1,4,3,6,2);
for($i=0;$i<count($number); $i+=2){
$dif[] = $number[$i] - $number[$i+1];
}
print_r($dif);
$sum = 0;
foreach ($dif as $item){
$sum += $item;
}
echo 'SUM = '.$sum;
?>
Working CODE
If you want all the different stages kept,
$numbers = [2,1,4,3,6,2];
$diff = [];
for($i=0,$c=count($numbers);$i<$c;$i+=2)
{
$diff[] = $numbers[$i]-$numbers[$i+1];
}
$sum = array_sum($diff);
Else, to just get the total and bypass the diff array:
$numbers = [2,1,4,3,6,2];
$total = 0;
for($i=0,$c=count($numbers);$i<$c;$i+=2)
{
$total += $numbers[$i]-$numbers[$i+1];
}
I have got this far it gives the required solution.
$arr = array(2,1,4,3,6,2);
$temp = 0;
$diff = array();
foreach ($arr as $key => $value) {
if($key % 2 == 0) {
$temp = $value;
}
else {
$diff[] = $temp - $value;
}
}
print_R($diff);
print 'Total :' . array_sum($diff);
Note : Please update if any one knows any pre-defined function than can sorten this code.
Please check and see if this works for you.
<?php
$sum=0;
$number = array(2,1,4,3,6,2);
for ($i=0;$i<=count($number);$i++) {
if ($i%2 == 1 ) {
$sum = $sum + $number[$i-1] - $number[$i];
}
}
print $sum;
?>
Well with your conditions in mind I came to the following
$number = [2,1,4,3,6,2];
$total = 0;
for($i = 0; $i < count($number); $i+=2) {
$total += $number[$i] - $number[$i + 1];
}
Try this one:
$number = array(2,1,4,3,6,2);
$diff = array();
$v3 = 0;
$i=1;
foreach($number as $val){
if ($i % 2 !== 0) {
$v1 = $val;
}
if ($i % 2 === 0) {
$v2 = $val;
$diff[] = $v1-$v2;
$v3+= $v1-$v2;
}
$i++;
}
print $v3;//total value
print_r($diff); //diff value array
I want all of the elements in the array to be added together, but this doesn't seem to be working.
<?php
function mimic_array_sum($array) {
foreach($array as $total) {
$total = $total + $total;
}
return $total;
}
$var = array(1,2,3,4,5);
$total = mimic_array_sum($var);
echo $total;
?>
$total = $total + $total --> well, there's your problem...
The $total variable gets overwritten on each loop through the array.
Assign a separate variable for each number in the array, like so:
function mimic_array_sum($array) {
$total = 0;
foreach($array as $number) {
$total = $total + $number;
}
return $total;
}
$var = array(1,2,3,4,5);
echo mimic_array_sum($var);
Although the point of this is not clear to me... You might as well use the php-function array_sum...
$var = array(1,2,3,4,5);
echo array_sum($var);
$var = array(1,2,3,4,5);
$total = array_reduce(
$var,
function($sum, $value) {
return $sum + $value;
}
);
though why not simply use array_sum()?
You can use array_sum — Calculate the sum of values in an array
$var = array(1,2,3,4,5);
$total = array_sum($var);
echo $total;
<?php
function mimic_array_sum($array) {
$total = 0;
foreach($array as $elem) {
$total += is_numeric($elem) ? $elem : 0;
}
return $total;
}
$var = array(1,2,3,4,5);
$total = mimic_array_sum($var);
echo $total;
?>
<?php
$a = array(1,2,3,4,5);
echo "sum is:".array_sum($a);
?>
See Manual
Please try following, you have to take separate variable to do so. Or else you can use array_sum()
function mimic_array_sum($array) {
$test = 0;
foreach($array as $total) {
$test = intval($test) + intval($total);
}
return $test;
}
$var = array(1,2,3,4,5);
$total = mimic_array_sum($var);
echo $total;
?>
Looking to calculate average of $data array. I need to first find the sum of the values in the array. Here's what I have but it doesn't seem to work.
$sum = 0;
foreach($data as $value) {
$sum = $sum+$value;
return $sum;
}
$count = count($data);
$average = $sum / $count;
echo "Average is $average <br />";
Try this:
$total = array_sum($data);
$average = $total / count($data);
Or, if you like one-liners:
$average = array_sum($data) / count($data);
remove the return
$sum = 0;
foreach($data as $value) {
$sum = $sum+$value;
}
$count = count($data);
$average = $sum / $count;
echo "Average is $average <br />";
The reason it's failing is that you shouldn't have the return statement. return is only used for returning from functions.
That said, you can just use array_sum() instead.