![]() The first one is the array, the second one is a callback with two parameters, the collector, and the current item. The arrayreduce function takes two ( sometime three ) parameters. SELECT SUM(p.product_price * c. You could use the arrayreduce to reduce your array to only one element. In the case that there's a quantity field, just change the SUM() function to multiply price by quantity: JOIN product AS p ON p.product_id = c.p_id SELECT SUM(p.product_price) AS total FROM cart AS c Assuming you don't have a "quantity" field, the function can be as simple as one SQL query with a JOIN and using the SUM() function. Your code looks much more complicated than it needs to be. You're overwriting the variable $sql and $row inside the loop that's using those variables for iteration, you're also overwriting the value of $total on each iteration. ![]() I'm only getting the latest price not the total price. The product_price in other table which is product. I'm trying to get total price from cart table. $sql = $db->query("SELECT product_price from product WHERE product_id='$product_id'") $no=$sql->rowCount() // number of rows affected by the last SQL statement $sql = $db->query("SELECT * from cart WHERE ip_add='$ip'") In PHP 5.I'm trying to get total price from cart table. $itemsQty = array_map('mapArray', $items) This function returns sum of all array elements integer or float. You can use this to simply return values of a specific key in the array: 4 Answers Sorted by: 4 arraysum () will do the job for you. Examples Example 1 arraysum () examples ![]() If array contains items of different datatypes, only the. Return Values Returns the sum of values as an integer or float 0 if the array is empty. The PHP arraysum() function computes sum (addition) of all numbers in an array and returns the result. Using the array_map() function, you can apply a callback function to each element of the array. arraysum () returns the sum of values in an array. You can then pass the resulting array as an argument to the array_sum() function to calculate and return the sum of all values in that array: Using the array_column() function, you can get an array of all values from a single column in the input array:Įcho print_r($itemsQty) // Array ( => 2 => 3 ) You can simply use the foreach loop to sum values of a specific key in an array, for example, in the following way:Īlthough the code spans a few lines, this would be the fastest way as it's using the language construct foreach rather than a function call (which is more expensive). ![]() To sum the values of a specific key (such as qty) in the array, you can use any of the following: This tutorial demonstrates the uses of arraysum() and how to sum the values of a multidimensional array. Salathe Edit: A simple (relatively) way to filter the keys and to sum the values (without writing a custom Iterator) would be to do some filtering with a RegexIterator, convert the resulting iterator into an array and use the handy arraysum function on it. Let's suppose you have an array of items like the following: The arraysum() returns an integer or float value after adding all the values if the array is empty, it will return 0. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |