Narix Posted February 29, 2012 Report Share Posted February 29, 2012 Lieta sekojoša, ir atlasīti dati no datubāzes un ielikti masīvā: $query = "SELECT * FROM kurss WHERE valuta='GBP' AND date BETWEEN '".$newdate."' AND '".$date."' "; $resultID = mysql_query($query) or die("Data not found."); $data = array(); while ($row = mysql_fetch_array($resultID)) { array_push($data, $row["kurss"]); } Ārā es dabūju šādus datus: Array ( [0] => 0.83700000 [1] => 0.83900000 [2] => 0.83900000 [3] => 0.84600000 [4] => 0.84600000 [5] => 0.84600000 [6] => 0.84600000 [7] => 0.84400000 [8] => 0.83900000 [9] => 0.83900000 [10] => 0.83100000 [11] => 0.83100000 [12] => 0.83100000 [13] => 0.82700000 [14] => 0.82900000 [15] => 0.82800000 ) Problēma ir tur, ka vajag no no 0-tā elementa atņemt pirmo, no 1-mā atņemt otro, no 2-trā trešo u.t.t.. Un šo iegūto satrpību starp katru no elementiem atkal ielikt masīvā ar iegūtajām elementu starpību vērtībām. Tātad būtu nepieciešams Array ( [0] => -0.002 [1] => 0 [2] => -0.007 [3] =>utt ) Kā lai kaut ko tādu vispār izdara? Quote Link to comment Share on other sites More sharing options...
marcis Posted February 29, 2012 Report Share Posted February 29, 2012 easy game ? <? $new = array(); foreach($array as $key=>$value){ if(array_key_exists($key+1, $array)){ $new[] = $value-$array[$key+1]; } } ?> Quote Link to comment Share on other sites More sharing options...
edw Posted February 29, 2012 Report Share Posted February 29, 2012 (edited) marcis, pasteidzās :) Edited February 29, 2012 by edw Quote Link to comment Share on other sites More sharing options...
Narix Posted February 29, 2012 Author Report Share Posted February 29, 2012 Tapēc jau tēma domāta iesācējiem. Paldies. :) Kā reize sāku skatīties foreach dokumentāciju. Quote Link to comment Share on other sites More sharing options...
aika Posted March 1, 2012 Report Share Posted March 1, 2012 īsāk! $query = "SELECT * FROM kurss WHERE valuta='GBP' AND date BETWEEN '".$newdate."' AND '".$date."' "; $resultID = mysql_query($query) or die("Data not found."); $data = array(); $value = mysql_fetch_array($resultID)[0]; // ?? while ($row = mysql_fetch_array($resultID)) { array_push($data, $value - $row["kurss"]); $value=$row["kurss"]; } Quote Link to comment Share on other sites More sharing options...
marcis Posted March 1, 2012 Report Share Posted March 1, 2012 @aika, doma teorētiski ir pareiza, ka nevajag taisīt lieku masīvu, bet konkrētais piemērs gan īsti nestrādās. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.