PHP - Summing an array of decimal values - php

I'm trying to calculate the sum of an array of decimal values in PHP, but for some reason it keeps rounding to integers.
for example:
$oldArray = array(0.00,1000.11,988.92,978.22,964.01,953.07,948.82,917.26,902.56,913.21,904.08,898.86,892.79);
$myVar = 0.0;
for($k=1;$k<10;$k++)
{
$myVar += $oldArray[$k];
}
print_r($myVar);
$oldArray is actually populated with decimal values from an SQL query (the length of $oldarray is about several hundred, but I want the first 10.
In the above example, I'm expecting $myVar to be a decimal, but it turns out to be just an integer. I tried setting $myVar = 0.0000 before the for loop, I tried $myVar += $oldArray[$k] + 0.0000, etc but nothing seems to work.
What am I doing wrong? How do I explicitly set $myVar to be a decimal?

Given that this seems impossible to reproduce, to me it sounds like a problem with your PHP environment itself.
Check php.ini for a setting called "precision", and make sure it's set to the default of 14 significant figures. I can't imagine why this would be changed, but it would definitely have an impact.

You can try using array_sum() instead and use (float) to cast the values. Additionally I would make sure that the values in the array are in the correct format (1.45 and not 1,45). HTH.
Update
Btw. you can use "is_float()" to check every parameter in the array.

Can't reproduce this.
php > $oldArray = array(0, .1, .2, .3, .4, .5, .6, .7, .8, .9);
php > $myVar = 0.0;
php > for($k=0;$k < count($oldArray);$k++)
php > {
php { $myVar += $oldArray[$k];
php { }
php > print_r($myVar);
4.5
EDIT: I tried the code in your comment, and it's fine. Like AlbertoPL, I suspect the problem is elsewhere.
php > $oldArray = array(0.01,1000.11,988.92,978.22,964.01,953.07,948.82,917.26,902.56,913.21,904.08,898.86,892.79);
php > $myVar = 0.0000;
php > for($k=1;$k<10;$k++)
php > $myVar += $oldArray[$k];
php > print_r($myVar);
8566.18

Make your own implementation:
function sum_array($arr){
$count = 0;
foreach ($arr as $val){
if (!is_numeric($val) // neglect any non numeric values
{
$error = true;
continue;
}
else{
$count = $count + ($val*1); //casting to numeric if the value supplied as string
}
}
return $count
}
echo sum_array($myArray);

Related

Counting a number and push each number into an array

I am trying to figure out how to count a number that I pull from query string and push each into an array. so if the number is 3, I want to push 1, 2 and 3 as separate numbers into the array. The below code does not work:
$number = $_GET['tics'];
$items = array();
for($numbers = 0; $numbers<$number; $numbers++) {
$items[] = $numbers;
}
var_dump shows an empty array with this code. any idea how to make this work?
I want the key to be "numbers" and the values to be 1, 2, 3 etc..
I am sure this is explained many times already on stack, but when searching I found only examples that was way to advanced for someone like me
you can use range()
Returns an array of elements from start to end, inclusive.
$number = (int) $_GET['tics'];
$items = range(1, $number);
You can use http://php.net/manual/en/function.range.php to generate a list of numbers from min to max.
<?php
$number = 5;
// Do validation on $number before passing it to range.
$result = range(1, $number);
print_r($result);
For starters, your loop is incorrect.
If you pass $number = 3
for ($numbers = 0; $numbers < $number; $numbers++) {
Will give you 0, 1, 2. You need to change it to the following:
for ($numbers = 1; $numbers <= $number; $numbers++) {
This will give you 1, 2, 3.
Anyway, entertaining another idea here, if range() as mentioned by the other answers is not what you require.
I want the key to be "numbers" and the values to be 1, 2, 3 etc..
It's not exactly clear what you mean by this, but I'm guessing you might want the array keys to be the value of $numbers? In which case, you can modify your code as follows:
$number = (int)$_GET['tics'];
$items = array();
for ($numbers = 1; $numbers <= $number; $numbers++) {
$items[$numbers] = $numbers;
}
Your code should somewhat work as intended anyway (the numbers will be incorrect). The reason it doesn't is probably that $_GET['tics'] has no value. Ensure that you do indeed have tics in the $_GET array, and that you aren't actually POSTing (in which case you need $_POST['tics'] instead).
If you change $_GET in your code to $_POST and it still doesn't work, then tics is either not set or does not have a value greater than 0.

PHP non-numeric value warning

I'm getting the following error:
Warning: A non-numeric value encountered
Does anyone have the solution? (I'm a beginner.)
EDIT:
Here is the code:
while($r = mysqli_fetch_row($result))
{
$items[] = $r;
}
$itemArray = array($items[0][0]=>array('Proid' => $items[0][0],
'name'=>$items[0][1], 'brand'=>$items[0][2], 'desc'=>$items[0][3],
'quan'=>$_POST["quantity"], 'price'=>$items[0][4], 'image'=> $items[0][5]));
$_SESSION['cart'] += $itemArray;
$i = $item['Proid'];
$tp = $item['quan'] * $item['price'];
$tp2 += $tp;
Probably some of your values are a string, not a integer or float.
To make sure, use
intval() to convert a string to integer
or
floatval() to convert a string to float.
This is just a warning, php will try to convert it behind the scenes, but if may not be accurate, for example, if you use , as decimal separator, or if you use some thousand separator.
Not sure which PHP version you are using but a Warning will be emitted if a non-numeric value is encountered. See this link.
so what you can do is before doing calculation make sure you check if the entity is numeric or not and for that you can use is_numeric() PHP function.
For example in your case :
if(is_numeric($item['quan']) && is_numeric($item['price'])){
$tp = $item['quan'] * $item['price'];
}

Check if cast to int will lose data - PHP

I have this piece of code:
$result = new stdClass();
foreach ($array as $index => $value) {
if(is_numeric($value)){
$int = (int)$value;
$double = (double)$value;
if($int == $double)
$value = $int;
else
$value = $double;
}
$index = strtolower($index);
$result->$index = $value;
}
And it worked for ages. Now I got a problem with it. I have a column in my database that has numbers (big numbers) in it. But they're not numbers, they're varchar and those numbers are not for mathematical purpose. Unfortunately, since the column is fully filled with numbers only, it passes the is_numeric test, but since it's a giant number, it loses data due to memory limitation (4 billions, I think).
Anyway, how can I check if after the cast I lost data or not to my variable?
Thanks.
if($value<=PHP_INT_MAX) ... // safe to convert
else // not safe
Convert it back and see if it gives the same value as the source.

PHP Undefined offset error repeated

I'm trying to run a PHP script that finds all the numbers divisible by 3 or 5, dumps them into an array, and adds all the values together. However, When I try to run it, I get a number output (I don't know if it's correct or not) and several hundred errors. They start out with:
Notice: Undefined offset: 1 in G:\Computer Stuff\WampServer\wamp\www\findthreesandfives.php on line 18
Then the offset number increases by increments of 1-3 (randomly, I haven't seen a pattern yet). I can't figure out what's wrong. Here's my code:
<?php
function loop($x)
{
$a = array(); //array of values divisible by 3 or 5
$l = 0; //length of the array
$e = 0; //sum of all the values in the array
for ($i=0; $i<=$x; $i++){ //this for loop creates the array
$n3=$i%3;
$n5=$i%5;
if($n3 === 0 || $n5 === 0){
$a[$i]=$i;
$l++;
}
}
for ($v=0; $v<=$l; $v++){ //this loop adds each value of the array to the total value
$e=$e + $a[$v];
}
return $e;
}
echo loop(1000);
?>
Someone please help...
The problem in your code is the following line:
$a[$i]=$i;
Should be:
$a[count($a)] = $i;
This is because the value of $i is always increasing, so using $i as your pointer will create gaps in the array's indices. count($a) returns the total number of items in the given array, which also happens to be the next index.
EDIT: #pebbl suggested using $a[] = $i; as a simpler alternative providing the same functionality.
EDIT 2: Solving the subsequent problem the OP described in the comments:
The problem seems to be that $l is greater than the number of items in $a. Thus, using count($a) in the for loop should fix your subsequent error.
Try replacing:
for ($v=0; $v<=$l; $v++){
With:
for ($v=0; $v<=count($a); $v++){
I found the same problem as #zsnow said. There are gaps within $a. The if condition allowed the gaps making the assignment skip some indexes. You can also use this
foreach ($a as $v){ //this loop adds each value of the array to the total value
$e=$e + $a[$v];
}
should actually be
foreach ($a as $v){ //this loop adds each value of the array to the total value
$e=$e + $v;
}

Which is proper form?

PHP.
$a['0']=1;
$a[0]=2;
Which is proper form?
In the first example you use a string to index the array which will be a hashtable "under the hood" which is slower. To access the value a "number" is computed from the string to locate the value you stored. This calculation takes time.
The second example is an array based on numbers which is faster. Arrays that use numbers will index the array according to that number. 0 is index 0; 1 is index 1. That is a very efficient way of accessing an array. No complex calculations are needed. The index is just an offset from the start of the array to access the value.
If you only use numbers, then you should use numbers, not strings. It's not a question of form, it's a question of how PHP will optimize your code. Numbers are faster.
However the speed differences are negligible when dealing with small sizes (arrays storing less than <10,000 elements; Thanks Paolo ;)
In the first you would have an array item:
Key: 0
Index: 0
In the second example, you have only an index set.
Index: 0
$arr = array();
$arr['Hello'] = 'World';
$arr['YoYo'] = 'Whazzap';
$arr[2] = 'No key'; // Index 2
The "funny" thing is, you will get exactly the same result.
PHP (for whatever reason) tests whether a string used as array index contains only digits. If it does the string is converted to int or double.
<?php
$x=array(); $x['0'] = 'foo';
var_dump($x);
$x=array(); $x[0] = 'foo';
var_dump($x);
For both arrays you get [0] => foo, not ["0"] => foo.
Or another test:<?php
$x = array();
$x[0] = 'a';
$x['1'] = 'b';
$x['01'] = 'c';
$x['foo'] = 'd';
foreach( $x as $k=>$v ) {
echo $k, ' ', gettype($k), "\n";
}0 integer
1 integer
01 string
foo string
If you still don't believe it take a look at #define HANDLE_NUMERIC(key, length, func) in zend_hash.h and when and where it is used.
You think that's weird? Pick a number and get in line...
If you plan to increment your keys use the second option. The first one is an associative array which contains the string "0" as the key.
They are both "proper" but have the different side effects as noted by others.
One other thing I'd point out, if you are just pushing items on to an array, you might prefer this syntax:
$a = array();
$a[] = 1;
$a[] = 2;
// now $a[0] is 1 and $a[1] is 2.
they are both good, they will both work.
the difference is that on the first, you set the value 1 to a key called '0'
on the second example, you set the value 2 on the first element in the array.
do not mix them up accidentally ;)

Categories