How does php foreach add up the value? The value of the third and fourth closures!

clipboard.png

clipboard.png


135.....

clipboard.png

I want to calculate the most expensive "price" * "amount"
can be multiplied normally, but there will be a problem with the sum

Mar.09,2021

$_sum = 0;
foreach ($sum as $key => $value) {
  $_sum += $key * $value;
}
echo $_sum;

. I don't think there's anything wrong with the Ls answer. It's obviously a summary value, so why cycle through echo ?

< hr >

I don't quite understand how your data structure is planned here. This should be a two-dimensional array structure.
in addition, you used number_format above. This output is a string. It is recommended that you use it before rendering the page after all calculations, because the computer's native floating-point numbers will have accuracy errors in calculation (you can search "IEEE754 accuracy error" by yourself), and the type is also wrong.


I don't understand your question, but I wonder if it has something to do with the reference value of foreach

  • How to break down the json, obtained from curl?

    I use php curl to retrieve json data from http , and then I use foreach array to display all the data , but when I encounter a problem, how to break it down? what kind of data does the counterparty need to give me? for example, he has a total of 30 yu...

    Feb.27,2021
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb497f-482be.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb497f-482be.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?