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
Menu