Sum of array in php, array generated from excel file - php

I am reading excel file in php. I want to show the total of one column in the last row. To do that I need to get the total sum of below array.
part of code to get the below array is,
Array ( [0] => 1 ) Array ( [0] => 2 )
$file = "$filename";
$sheet = (isset($_POST['sheet'])) ? $_POST['sheet'] : '';
$connection = new Spreadsheet_Excel_Reader();
$connection->read($file);
echo"<table>";
$x = 1;
while ($x <= $connection->sheets[$sheet]['numRows']) {
echo "\t<tr>\n";
$y=1;
while ($y <= $connection->sheets[$sheet]['numCols']) {
$cell = isset($connection->sheets[$sheet]['cells'][$x][$y]) ? $connection->sheets[$sheet]['cells'][$x][$y] : '';
echo "\t\t<td>$cell</td>\n";
$y++;
}
echo "\t</tr>\n";
$x++;
}
$x = 2;
while ($x <= $connection->sheets[$sheet]['numRows']) {
$ctnqty = isset($connection->sheets[$sheet]['cells'][$x][16]) ?
$connection->sheets[$sheet]['cells'][$x][16] : '';
$ctnqtyttl = explode(",",$ctnqty);
print_r ($ctnqtyttl);
$x++;
}
I'm suing below code to get the sum of above array, but its giving me the result of 2 only. Please help me here to get the sum 3.
$sum = 0;
foreach ($ctnqtyttl as $value) {
echo $sum = $sum + $value;
}

$ctnqtyttl is overwritten each time with a new value in your second while loop, so it forgets any previous values. The sum you calculate will only be based on the last value it has.
Skip the final foreach loop, and calculate the sum during the second while loop as follows:
$sum = 0; // <-------
$x = 2;
while ($x <= $connection->sheets[$sheet]['numRows']) {
$ctnqty = isset($connection->sheets[$sheet]['cells'][$x][16]) ?
$connection->sheets[$sheet]['cells'][$x][16] : '';
$ctnqtyttl = explode(",", $ctnqty);
print_r ($ctnqtyttl);
$sum += array_sum($ctnqtyttl); // <-------
$x++;
}

Related

Problem with php array function displaying only the first item in the array ignoring the rest

I am trying to make a function to display client's items in an array, however only the first item id is shown in the array. What's wrong with my code? I guess I'm doing something wrong with the array.
Example of sItem value: https://pastebin.pl/view/e492ffa1
$items = array();
$client_item = bin2hex($u->sItem);
$x = 0;
for ($i = 0; $i < 78; $i++) {
$item = hexdec(reverse(substr($client_item, $x, 8)));
if ($item != 0) {
$ii = $db->get_object("SELECT * FROM Clients.dbo.ITEM WHERE Num =" . $item);
if (is_object($ii)) {
$items[] = array("ItemID" => $ii->Num, "ItemSlot" => $i);
}
}
$x += 16;
}
The code above will show just the first item in the array. All others are not shown.
var items = [{"ItemID":310511133,"ItemSlot":0}]
I am not sure what I'm doing wrong. If I remove $x += 16; then it will simply add more entries in the array with the first item id. I want more entries but not with the same item id obviously. :)
var items = [{"ItemID":310511133,"ItemSlot":0},{"ItemID":310511133,"ItemSlot":1},{"ItemID":310511133,"ItemSlot":2},{"ItemID":310511133,"ItemSlot":3},{"ItemID":310511133,"ItemSlot":4}....
This is the reverse function:
function reverse($str)
{
$len = strlen($str);
$i = $len - 2;
$ret = NULL;
while (0 <= $i) {
$ret .= substr($str, $i, 2);
$i -= 2;
}
return $ret;
}
Database Query not returning any object SELECT * FROM Clients.dbo.ITEM WHERE Num = $item
can you confirm values exists in database
you can echo $item value and see manually in database are those values exists.

How can I sum up values from a json string?

I get the following json data from a database:
[{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}][{"type":"Sig","value":"0.0"},{"type":"SH","value":"9.95"},{"type":"COD","value":"6.95"}]
I'm trying to add all value values together, so: 9.95 + 6.95 ... so that I get 67.6 as result.
I tried the below code, but I am getting 16.9 as repeated values.
for ($i = 0; $i <= $count - 1 ; $i++) {
$charge = $service[$i]['charge'];
$serviceValue = json_decode($charge, true);
$totalservice = 0;
foreach ($serviceValue as $key => $value) {
$totalservice += $value['service_value'];
}
echo $totalservice;
}
You can do it like below:-
$jsonObj = json_decode($json); // Decode the JSON to OBJ
// Now loop and find the SUM
$total = 0;
foreach ($jsonObj as $item){
$total =+ $item->value;
}
// Print the SUM
echo "Sum : $total";
Note:- In your code $totalservice beome 0 every time when loop goes to next iteration and that's why you are getting same value repeated time. So do like (what #u_mulder said) :-
$totalservice = 0;
for ($i = 0; $i <= $count-1 ; $i++) {
.....//rest code
}
I have made the below changes. It works fine.
$totalservice = 0;
for ($i = 0; $i <= $count-1 ; $i++) {
$charge = $service[$i]['charge'];
$serviceValue = json_decode($charge, true);
foreach ($serviceValue as $key => $value) {
$totalservice+= $value['service_value'];
}
echo $totalservice;
}
Thanks for the help

Calculate from an array the number that is equal or higher and closest to a given number

I need to calculate from a given array the number that is equal or higher and closest to a given number in PHP. Example:
Number to fetch:
6.85505196
Array to calculate:
3.11350000
4.38350000
4.04610000
3.99410000
2.86135817
0.50000000
Only correct combination should be:
3.99410000 + 2.86135817 = 6.85545817
Can somebody help me? It's been 3 hours I'm getting mad!
UPDATE: I finally finished my code as following:
$arr = array(3.1135, 4.3835, 4.0461, 3.9941, 2.86135817, 0.5);
$fetch = 6.85505196;
$bestsum = get_fee($arr, $fetch);
print($bestsum);
function get_fee($arr, $fetch) {
$bestsum = 999999999;
$combo = array();
$result = array();
for ($i = 0; $i<count($arr); $i++) {
combinations($arr, $i+1, $combo);
}
foreach ($combo as $idx => $arr) {
$sum = 0;
foreach ($arr as $value) {
$result[$idx] += $value;
}
if ($result[$idx] >= $fetch && $result[$idx] < $bestsum) $bestsum = $result[$idx];
}
return $bestsum;
}
function combinations($arr, $level, &$combo, $curr = array()) {
for($j = 0; $j < count($arr); $j++) {
$new = array_merge($curr, array($arr[$j]));
if($level == 1) {
sort($new);
if (!in_array($new, $combo)) {
$combo[] = $new;
}
} else {
combinations($arr, $level - 1, $combo, $new);
}
}
}
I hope the following example might help you. Please try this
<?php
$array = array(
"3.11350000",
"4.38350000",
"4.04610000",
"3.99410000",
"2.86135817",
"0.50000000"
);
echo "<pre>";
print_r($array);// it will print your array
for($i=0; $i<count($array); $i++)
{
$j=$i+1;
for($j;$j<count($array); $j++)
{
$sum = $array[$i] + $array[$j];
// echo $array[$i]. " + ".$array[$j]." = ".$sum."<br>"; //this will display all the combination of sum
if($sum >= 6.85505196 && ($sum <= round(6.85505196)) )//change the condition according to your requirement
{
echo "The correct combinations are:<br/><br/>";
echo "<b>". $array[$i]. " + ".$array[$j]." = ".$sum."<b>";
echo "<br/>";
}
}
echo "<br/>";
}
?>
We will get the result as below
Array
(
[0] => 3.11350000
[1] => 4.38350000
[2] => 4.04610000
[3] => 3.99410000
[4] => 2.86135817
[5] => 0.50000000
)
The correct combinations are:
4.04610000 + 2.86135817 = 6.90745817
3.99410000 + 2.86135817 = 6.85545817
You should do it in two steps:
a. Work out (or look up) an algorithm to do the job.
b. Implement it.
You don't say what you've managed in the three hours you worked on this, so here's a "brute force" (read: dumb) algorithm that will do the job:
Use a variable that will keep your best sum so far. It can start out as zero:
$bestsum = 0;
Try all single numbers, then all sums of two numbers, then all sums of three numbers, etc.: Every time you find a number that meets your criteria and is better than the current $bestsum, set $bestsum to it. Also set a second variable, $summands, to an array of the numbers you used to get this result. (Otherwise you won't know how you got the solution). Whenever you find an even better solution, update both variables.
When you've tried every number combination, your two variables contain the best solution. Print them out.
That's all. It's guaranteed to work correctly, since it tries all possibilities. There are all sorts of details to fill in, but you can get to work and ask here for help with specific tasks if you get stuck.
Thank you all for your help!
My code is working pretty cool when is needed to fetch one or two numbers (addition) only. But can't figure out how to add more combinations up to the total count of elements in my given array.
I mean if there are, let's say, 8 numbers in my array I want to try all possible combinations (additions to each other) as well.
My actual code is:
$bestsum = 1000000;
for ($i = 0; $i < count($txinfo["vout"]); $i++) {
if ($txinfo["vout"][$i]["value"] >= $spent && $txinfo["vout"][$i]["value"] < $bestsum) {
$bestsum = $txinfo["vout"][$i]["value"];
}
}
for($i = 0; $i < count($txinfo["vout"]); $i++) {
$j = $i + 1;
for($j; $j < count($txinfo["vout"]); $j++) {
$sum = $txinfo["vout"][$i]["value"] + $txinfo["vout"][$j]["value"];
if($sum >= $spent && $sum < $bestsum) {
$bestsum = $sum;
}
}
}
$fee = bcsub($bestsum, $spent, 8);
print("Fee: ".$fee);
New updated code.
<?php
$x = 6.85505196;
$num = array(3.1135, 4.3835, 4.0461, 3.9941, 2.86135817, 0.5);
asort($num); //sort the array
$low = $num[0]; // lowest value in the array
$maxpossible = $x+$low; // this is the maximum possible answer, as we require the number that is equal or higher and closest to a given number
$num = array_values($num);
$iterations = $x/$num[0]; // possible combinations loop, to equate to the sum of the given number using the lowest number
$sum=$num;
$newsum = $sum;
$k=count($num);
for($j=0; $j<=$iterations; $j++){
$l = count($sum);
for($i=0; $i<$l; $i++){
$genSum = $sum[$j]+$sum[$i];
if($genSum <= $maxpossible){
$newsum[$k] = $genSum;
$k++;
}
}
$newsum = array_unique($newsum);
$newsum = array_values($newsum);
$k = count($newsum);
$sum = $newsum;
}
asort($newsum);
$newsum = array_values($newsum);
for($i=0; $i<count($newsum); $i++){
if($x<=$newsum[$i]){
echo "\nMaximum Possible Number = ".$newsum[$i];
break;
}
}
?>

PHP while($x = mysql_fetch_array($y))

I'm using in PHP
while($x = mysql_fetch_array($y))
I want to select and show 3 records from my MySQL DB, than make in HTML code and show next 3 records from database.
How to do this?
$idx = 0;
$htmlStr = '<div>';
while($x = mysql_fetch_array($y)) {
if($idx % 3 == 0 && $idx>0) {
//here your part when 3 rows areready
echo $htmlStr.'</div>';
$idx = 0;
$htmlStr = '<div>';
}
$idx++;
$htmlStr .= '<p>'.implode(',',$x).'</p>';
}
if($idx % 3 == 0 ) {
echo $htmlStr.'</div>';
}
Apart from using LIMIT 3 in sql query, you can add counter:
$c = 0;
$e = $c + 3;
while($x = mysql_fetch_array($y) && $c < $e){
...
$c++;
}
Of course you can put that into another loop that will increment the initial $c value by 3 and continue retrieving values from this query result.

How to find the mode of an array in PHP

I have an array that has been sorted from low to high which has over 260k values in. I have found out the mean(average) and median of the array just need to find out the mode?
I cannot use any mathematical functions that PHP has, it has to be all done manually.
I would like it so there could be just one value that is the mode but then there can be multiple values that can be the mode. I also need to be able to record the number of times that the value is stored. For example the number 51 appears 6 times so I can print both values.
This is my code so far:
$amountRecords = 0;
$totalValue = 0;
$valueArray = array();
// reads in csv file
$handle = fopen('Task1-DataForMeanMedianMode.csv', 'r');
// to skip the header names/values
fgetcsv($handle);
// creates array containing variables of csv file in ascending order
while(($row = fgetcsv($handle, "\r")) != FALSE)
{
// if amountRecords equals 0
if($amountRecords == 0)
{
// adds value from csv to array
$valueArray[] = $row[1];
} // else amountRecords does not equal 0
else
{
// if the value in array location before is greater than the current value from the csv file
if($valueArray[$amountRecords - 1] > $row[1])
{
// the current array location becomes the one in the location before
$valueArray[] = $valueArray[$amountRecords - 1];
// add the value from the csv to the location before
$valueArray[$amountRecords - 1] = $row[1];
} // if the value in the location before is not greater than the current value in the csv file
else
{
// adds value from csv to array
$valueArray[] = $row[1];
}
}
// calculates the total value of the values in csv file
$totalValue = $totalValue + $row[1];
// calculates the number of values in the csv file
$amountRecords++;
}
// calculate average value of payments
$averageValue = $totalValue / $amountRecords;
// limit integer to 2 decimal place
$average = number_format($averageValue,2,'.','');
// finds middle value
$middle = floor(($amountRecords / 2) - 1);
// calculates the median value
// if array size is even
if($amountRecords % 2 == 0)
{
// calculates median
$median = $valueArray[$middle];
}
else // if array size is odd
{
// calculate low and high values
$low = $valueArray[$middle];
$high = $valueArray[$middle + 1];
// calculates median
$median = (($low + $high) / 2);
}
// works out mode
// creates array count
$count = array();
// for each value in the valueArray
foreach( $valueArray as $value )
{
if( isset( $count[$value] ))
{
$count[$value]++;
}
else
{
$count[$value] = 1;
}
}
$mostCommon = "";
$iter = 0;
foreach( $count as $k => $v )
{
if( $v > $iter )
{
$mostCommon = $k;
$iter = $v;
}
}
$modeArray = array( "mode" => $mostCommon , "count" => $iter );
The mode of a numerical set is the number that occurs the most often. You can do this with PHP using code similar to the following:
$values = array_count_values($valueArray);
$mode = array_search(max($values), $values);
Simple!
$arr = array(4,6,7,1,4,7,4,7,1);
$freq = array();
for($i=0; $i<count($arr); $i++)
{
if(isset($freq[$arr[$i]])==false)
{
$freq[$arr[$i]] = 1;
}
else
{
$freq[$arr[$i]]++;
}
}
$maxs = array_keys($freq, max($freq));
for($i=0; $i<count($maxs); $i++)
{
echo $maxs[$i] . ' ' . $freq[$maxs[$i]];
echo '<br />';
}
Mathematical only solution:
//sample data
$dataArr = ["1", "3", "5", "1", "3", "7", "1", "8", "1"];
//a multidimensional array to hold the keys (extracted fro above) and their values (number of occurrences)
$multiDArr = [];
for ($i = 0; $i < count($dataArr); $i++) {
$key = $dataArr[$i];
if (isset($multiDArr[$key])) {
//key already exists; increment count of its value
$multiDArr[$key] = $multiDArr[$key] + 1;
} else {
//key does nto exist; add it and an inital value of 1
$multiDArr[$key] = 1;
}
}
$highestOccuring = 0;
$highestOccuringKey = null;
foreach ($multiDArr as $key => $value) {
if ($value > $highestOccuring) {
$highestOccuring = $value;
$highestOccuringKey = $key;
}
}
echo "MODE / highest occuring key: " . $highestOccuringKey;
/** find array mode, most often see value
* #param list(int) $a_in
* #return list(int)
*/
function array_mode(array $a_in): array{
$a_freq = [];
foreach( $a_in as $v ) {
if (!isset($a_freq[$v])) {
$a_freq[$v] = 0;
}
$a_freq[$v]++;
}
$a_maxs = array_keys($a_freq, max($a_freq));
return $a_maxs;
}
// test code
$a_in = [4,6,7,1,4,7,4,7,1];
array_mode( $a_in);
This will take values and turn it into an array of the modes. For Example: print_r(get_mode(1,2,2,3,3,3,4,4,5,5,5,6,7,8,9)); will return,
Array
(
[0] => Array
(
[value] => 3
[count] => 3
)
[1] => Array
(
[value] => 5
[count] => 3
)
)
code:
function get_mode(...$inputArray){
$max=0;
return array_reduce(
array_values(array_reduce(array_values($inputArray),function($talliedArray,$inputNode){
if(isset($talliedArray[(string)$inputNode]))
$talliedArray[(string)$inputNode]['count']++;
else
$talliedArray[(string)$inputNode] = [
'value' => $inputNode,
'count' => 1
];
return $talliedArray;
},[])),function($modeArray,$talliedNode)use(&$max){
if($talliedNode['count'] < $max) return $modeArray;
if($talliedNode['count']=== $max) return array_merge($modeArray,[$talliedNode]);
//if count > $max
$max = $talliedNode['count'];
return [$talliedNode];
},[]);
}
This satisfies the "no math functions", the "multiple return modes" and the "have the value and number of occurrences returned".
This will only really work with strings and numbers. It will go weird with booleans, Objects and Arrays.

Categories