last day (15 days later) » 

8:55 PM
-2
Q: Cumulative Multiplication (Product) in PHP of FOREACH loops

rainerbrunotteI extract values from an XML with PHP <?php $url = 'list.xml'; $xml = simplexml_load_file($url); $entries = $xml->item; $i = 0; $total = 1; foreach($entries as $entry){ $i++; $number[$i] = $entry->total; $total *= $number[$i]; } echo $total; ?> How can I build a total based on each $number e...

 
initialize $total before the loop with 0 and in the loop use *= operator
 
hm, I get only 0 if I echo out the $total inside each loop: $total *= $number[$i]; and then echo $total;
 
1. $totoal = 0; before the foreach loop 2. in the foreach loop: $total *= $number[$i]; 3. after the foreach loop: echo $total; do you get 0 if you use this? (IF yes one of the number is 0)
 
yes, I still get 0. I have different numbers, but all are larger than 0, so like: 1.534, 12.934, 0.012, 0.1323, 6.124, ...
 
It has to work! can you please post what value all numbers have? If they are too many post only a few
 
8:55 PM
1.2471, 0.6356, 1.5143, 7.8182, 0.9735, 0.84, 19.2042, 0.0652, 1622.9036, 0.0871, 0.0012, 0.6101
 
lol sry my mistake xD! you have to initalize $total with 1! This was the number which was 0 :D it should work if you initalize $total with 1
 
lol, nope, tried that myself too earlier, still get 0 :D
 
The posted values are these all values or just a few?
 
these are all. none of the values i receive are 0. maybe i need to convert them again to a number format? which wouldn't make sense, since they come in as a number in the XML already: <total>1.2471</total>
 
So if you put a "echo" before the assignment in the loop you get these values? (Sry for the question, but it makes no sense to my, it should work)
 
8:55 PM
see updated code above. strangely, the first echo of $total for loop 1 is 1 but turns to 0 in loop 2 and i don't see why
 
Hey
 
if i use $total += $number[$i]; then i get a total of 1651 without any decimal places of all those values
maybe i need to set the decimal places manually within the loop ?
so since some are 0.0123 somehow PHP rounds them to 0
 
echo 0.02345; What output do you get with this line?
 
0.02345
(wtf)
 
okay
So if you change this line: $number[$i] = $entry->total;
to echo $number[$i] = $entry->total;
Then you get these values: 1.2471, 0.6356, 1.5143, 7.8182, 0.9735, 0.84, 19.2042, 0.0652, 1622.9036, 0.0871, 0.0012, 0.6101
?
(If the echo is 1 line you can use this: echo "<br />" . $number[$i] = $entry->total; )
 
9:01 PM
yes i get all those values, one by one separately, one for each loop
 
If you take this example code:

$array = array(1.2471, 0.6356, 1.5143, 7.8182, 0.9735, 0.84, 19.2042, 0.0652, 1622.9036, 0.0871, 0.0012, 0.6101);
$total = 1;

foreach($array as $value)
$total *= $value;

echo $total;

What output do you get?
 
i get the full multiplication of 0.99438845568625 as a grand total
 
No it get's strange xD
And this code gives you 0 ? (It's a little bit modified)
$url = 'list.xml';
$xml = simplexml_load_file($url);
$entries = $xml->item;
$i = 0;
$total = 1;
$number = array();
foreach($entries as $entry){
$i++;
$number[$i] = $entry->total;
$total *= $number[$i];
}
echo $total;
 
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 
?this code? It only has 1 echo statement?!
 
9:19 PM
ah sorry, i get 0
 
only 1 idea left...
Please post the content of the xml file here
 
i use the bid prices of this xml file query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xch‌​ange%20where%20pair%20in%20(%22EURUSD%22,%22USDGBP%22,%22GBPCHF%22,%22CHFSEK%22,%‌​22SEKNOK%22,%22NOKCNY%22,%22CNYJPY%22,%22JPYHKD%22,%22HKDIDR%22,%22IDRKRW%22,%22K‌​RWSGD%22,%22SGDUSD%22)&env=store://datatables.org/alltableswithkeys
 
Or make a pastbin
 
9:38 PM
Okay now i can reproduce your error! finally. now i have to solve it
 
oh wow thats awesome !
 
So
I have a solution:

$url = "list.xml";
$xml = simplexml_load_file($url);

$entries = $xml->results->rate;
$count = 0;
$total = 1;
$number = array();

foreach($entries as $entry){
$count++;
$number[$count] = $entry->Bid;
$total *= (double)$number[$count];
}

echo "Total: " . $total;
Does this work for you?
(Just cast it to double before using *=)
 
yep that works ! :) but what is this (double) for and why did this magically solve that? it should also work with normal multiplication no?
 
Yes it should work. And (double) makes a cast to that type So you say explicit: "Hey this value is a double"
(Made a answer)
 
ok, i have no idea what that means but i am glad that it works :)
thanks so much for your time and effort !
 
9:47 PM
found it :D
You have to cast the result of simplexml_load_file()
Take a look here: php.net/manual/en/…
 
what do you mean ?
oh it was considered a string rather than an integer and thats why i get 0 ?
 
yep
 
:D omg
 
and with (double) you say explicit hey that's a double
 
wtf but php should automatically detect this
 
9:52 PM
(Don't know why your question have down votes, think because people though the question is to simple or didn't understood it.)
 
i dont either ... maybe you can post that solution with the (double) ?
 
posted it ;D
Also changed the title so more people with the same problem find this faster :D
 
awesome ! thank you very very much for this !
 
Your welcome! Have a nice day :D
 
if it was a simple problem with declaring total = 1 at first, i wouldnt have posted the pb for sure lol
you too ! byebye
 
9:57 PM
bye
(Also would be nice if you could accept and upvote it :))
;)
 
obviously !
 
bye
 

last day (15 days later) »