Jump to content
php.lv forumi

Darbības ar masīviem


Narix

Recommended Posts

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?

Link to comment
Share on other sites

ī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"];
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...