how to sum amount in multiple json array in php

php sum array values by key
sum of array in php using for loop
php sum two array values by key
php sum array multidimensional
array sum php
sum values in foreach loop php
array sum js
php sum variables

how to sum amount in multiple json array. i my table every amount add in json array and i want to sum all amount from json array according to id please help

| id   | particulars | client_id

|  140 | [{"amt":"850","ptr":"ITR FEE"}] | 1872
| 1637 | [{"amt":"900","ptr":"ITR RET 2018-19"}] | 1872

my query like this:

  $fetchfbillamt = $ketObj->runquery("SELECT", "*", "vksoft_fbill", array(), "where client_id=".'1872'."");
if (isset($fetchfbillamt) && is_array($fetchfbillamt) && 
 count($fetchfbillamt) > 0) 
{
  $fbillencodeamt = $fetchfbillamt[0]['particulars'];
  $fbilldecodeamt = json_decode($fbillencodeamt);
  foreach ($fbilldecodeamt as $fbilldecodeamtV) 
  {
     $sumfbillamt +=$fbilldecodeamtV->amt;
  }
echo $sumfbillamt;
}

showing output 850

not showing 1750

You've written your foreach loop on the wrong variable, you should be iterating over $fetchbillamt instead. Try this:

if (isset($fetchfbillamt) && is_array($fetchfbillamt) && 
 count($fetchfbillamt) > 0) 
{
  foreach ($fetchbillamt as $billamt) {
    $fbillencodeamt = $billamt['particulars'];
    $fbilldecodeamt = json_decode($fbillencodeamt);
    $sumfbillamt += $fbilldecodeamt[0]->amt;
  }
  echo $sumfbillamt;
}

How to sum all column values in multi-dimensional array?, How do you sum values of an array with the different key in PHP? The array_sum () function returns the sum of all the values in the array.

You will made the mistake in following place, Result Is Multidimensional array but you assign the value in static way.

Try This code,

<?php
$fetchfbillamt = array(0=>array('id'=>140,
                     'particulars'=>'[{"amt":"850","ptr":"ITR FEE"}]'),
                      1=>array('id'=>1637,
                    'particulars'=>'[{"amt":"900","ptr":"ITR RET 2018-19"}]'));


$sum = 0;
foreach ($fetchfbillamt as $value){
    $sum += json_decode($value['particulars'],true)[0]['amt'];
}

?>

How to Calculate the Sum of Values in an Array in PHP, php sum two array values by key php sum array multidimensional array sum php array sum js php array php sum variables. how to sum amount in multiple json  I am retrieving JSON data in array and I sort them into the HTML table. So far it is OK, but I would like to sum the specific values from JSON Response and print the sum of object values. I have a PHP code which sorting me the data from JSON as following:

You can simplify your code using the array reduce function. We can create our own custom functions as required and do the operations.

$json = array(['amt' =>"850",'ptr'=>"ITR FEE"], ['amt'=>"900",'ptr'=>"ITR FEE"]);

function sum($carry, $item)
{
    $carry += $item['amt'];
    return $carry;
}


var_dump(array_reduce($json, "sum")); 

Will output int(1750)

how to sum amount in multiple json array in php, array_sum — Calculate the sum of values in an array. Description ¶. array_sum ( array $array ) : number. array_sum() returns the sum of values in an array. For the purpose of this article, we'll be using the following PHP associative array: $items = array('item_1' => array('id' => 1, 'name' => 'White Shirt', 'qty' => 2),'item_2' => array('id' => 2, 'name' => 'Blue Shirt', 'qty' => 3)); We'll be summing the values of the key qty, which should give us the result 5.

It looks like you are only taking the first row in your line:

$fbillencodeamt = $fetchfbillamt[0]['particulars'];

This should actually be in your foreach loop like so:

$fetchfbillamt = $ketObj->runquery("SELECT", "*", "vksoft_fbill", array(), "where client_id=".'1872'."");
if (isset($fetchfbillamt) && is_array($fetchfbillamt) && count($fetchfbillamt) > 0) 
{
  foreach ($fetchfbillamt as $fetchedbill) 
  {
      $fbillencodeamt = $fetchedbill['particulars'];
      $fbilldecodeamtV = json_decode($fbillencodeamt);
      $sumfbillamt += $fbilldecodeamtV['amt'];
  }
  echo $sumfbillamt;
}

array_sum - Manual, This is another way to get value from a multidimensional array, but for versions to count the total number of times a specific value appears in array, use this function: extract all values from a multi dimesnsional array or a nexted json object I'd like to sum the values in the "Sign Up" Object (10 + 10). The challenge I am having is that the key's are unique and most of the posts demonstrating how to do something like this show examples where it is just an array of values or the keys are consistent (ie - every key is "value").

array_values - Manual, Return Value: Returns the sum of all the values in an array. PHP Version: 4.0.4+. PHP Changelog: PHP versions prior to 4.2.1 modified the passed array itself  In this tutorial, you will learn how you can generate and parse JSON data in various ways using PHP. Encoding JSON data using PHP. PHP uses json_encode() method to convert PHP array into JSON data. Example-1: In the following example, an array is declared and after encoding the data will be generated in json format.

PHP array_sum() Function, We show you how to work with PostgreSQL JSON data and introduce you to some important PostgreSQL JSON JSON is an open standard format that consists of key-value pairs. Let's insert multiple rows at the same time. We can apply aggregate functions such as MIN, MAX, AVERAGE, SUM, etc., to JSON data. array_count_values (PHP 4, PHP 5, PHP 7) array_count_values — Counts all the values of an array

PostgreSQL JSON Tutorial, Multiple key/value pairs are separated by a comma , . Whereas an example of JSON array would look something like this: In PHP the json_encode() function is used to encode a value to JSON format. else{ $values[] = $value; $count++; } } // Return total count and values found in array return array('total' => $count,  array_sum (array $array) : number array_sum () returns the sum of values in an array.

Comments
  • You're not tempted to store 'decoded'/normalised data?
  • Why not do the summing in the MySQL query instead of PHP? SELECT SUM(particulars->"$.amt")
  • sir showing error Trying to get property 'amt' of non-object in
  • @shivsharma I had a typo which I've corrected (missing [0]). Please try again.
  • You should explain what's wrong with his code and how your code fixes it, not just say "try this"
  • Hey @Barmar is this okay.
  • How is this any different from the answer I posted 10 minutes earlier?
  • sir you right. but showing error Trying to get property 'amt' of non-object in
  • Sorry @Nick, I hadn't seen your answer when I was submitting mine. I guess I took a lot longer to write out the answer.
  • @shivsharma if it is not coming through as an object it would probably be $fbillcodeamtV['amt'], I will update my answer now. Nick had the right idea though