Autor Zpráva
Nat
Profil *
Zdravím, jaký by byl vhodný cyklus pro řešení tohoto zadání:

Mám toto pole:

Array ( [0] => 273 [1] => 3 [2] => 315 [3] => 2 [4] => 2 [5] => 2 [6] => 2 [7] => 2 [8] => 2 [9] => 2 [10] => 2 [11] => 2 [12] => 2 [13] => 6 [14] => 3 [15] => 6 [16] => 2 [17] => 1 [18] => 2 [19] => 2 [20] => 3 [21] => 4 [22] => 1 [23] => 0 [24] => 1 [25] => 2 [26] => 2 [27] => 0 [28] => 2 [29] => 2 [30] => 4 [31] => 0 [32] => 0 [33] => 0 [34] => 0 [35] => 1 [36] => 0 [37] => 0 [38] => 0 [39] => 0 [40] => 0 [41] => 0 [42] => 0 [43] => 1 [44] => 4 [45] => 1 [46] => 0 [47] => 0 [48] => 0 [49] => 0 [50] => 0 [51] => 0 [52] => 0 [53] => 2 [54] => 5 [55] => 0 [56] => 0 [57] => 0 [58] => 0 [59] => 0 [60] => 0 [61] => 0 [62] => 0 [63] => 0 [64] => 0 [65] => 0 [66] => 0 [67] => 0 [68] => 0 [69] => 0 [70] => 0 [71] => 0 [72] => 0 [73] => 0 [74] => 0 [75] => 0 [76] => 2 [77] => 4 [78] => 0 [79] => 0 [80] => 0 [81] => 0 [82] => 0 [83] => 0 [84] => 0 [85] => 1 [86] => 1 [87] => 0 [88] => 0 [89] => 0 [90] => 0 [91] => 2 [92] => 0 [93] => 0 [94] => 8 [95] => 0 [96] => 0 [97] => 2 [98] => 0 [99] => 0 [100] => 0 [101] => 0 [102] => 1 [103] => 0 [104] => 0 [105] => 0 [106] => 0 [107] => 0 [108] => 0 [109] => 0 [110] => 0 [111] => 1 [112] => 0 [113] => 0 [114] => 2 [115] => 1 [116] => 0 [117] => 0 [118] => 0 [119] => 1 [120] => 0 [121] => 2 [122] => 1 [123] => 0 [124] => 0 [125] => 1 [126] => 1 [127] => 0 [128] => 1 [129] => 0 [130] => 1 [131] => 0 [132] => 0 [133] => 0 [134] => 0 [135] => 0 [136] => 6 [137] => 1 [138] => 0 [139] => 0 [140] => 2 [141] => 1 [142] => 1 [143] => 0 [144] => 2 [145] => 0 [146] => 1 [147] => 0 [148] => 0 [149] => 0 [150] => 0 [151] => 3 [152] => 0 [153] => 0 [154] => 1 [155] => 3 [156] => 0 [157] => 2 [158] => 0 [159] => 1 [160] => 0 [161] => 0 [162] => 2 [163] => 2 [164] => 1 [165] => 2 [166] => 2 [167] => 0 [168] => 0 [169] => 1 [170] => 1 [171] => 1 [172] => 0 [173] => 0 [174] => 1 [175] => 2 [176] => 2 [177] => 3 [178] => 4 [179] => 0 [180] => 0 [181] => 0 [182] => 0 [183] => 3 [184] => 0 [185] => 0 [186] => 6 [187] => 0 [188] => 0 [189] => 1 [190] => 3 [191] => 4 [192] => 1 [193] => 0 [194] => 0 [195] => 0 [196] => 2 [197] => 3 [198] => 0 [199] => 0 [200] => 2 [201] => 2 [202] => 0 [203] => 3 [204] => 3 [205] => 2 [206] => 0 [207] => 8 [208] => 0 [209] => 0 [210] => 2 [211] => 1 [212] => 0 [213] => 0 [214] => 1 [215] => 0 [216] => 1 [217] => 4 [218] => 1 [219] => 0 [220] => 2 [221] => 5 [222] => 2 [223] => 0 [224] => 2 [225] => 1 [226] => 1 )

a potřebuji aby se od Array [2] (315) odečetlo Array [3] a od výsledku Array [2]-Array [3] se odečetlo Array [4] a tak dál. Toto vše by se opět ukládalo do pole ale už v konečných hodnotách a od Array [0] tedy výsledek by vypadal takto:

Array ( [0] => 315 [1] => 313 [2] => 311 [3] => 309 .......................)

Díky za nápady

Nat
Sir Tom
Profil
Nat:
Také zdravím

$novePole = array();
$vysledek = $mojeStarePole[2];
$novePole[] = $vysledek;

for($i=3; $i<count($mojeStarePole); $i++){
   $vysledek = $vysledek - $mojeStarePole[$i]; 
   $novePole[] = $vysledek;
}

Psáno z hlavy a nezkoušel jsem to...
Nat
Profil *
Vyřešil jsem to takto:

for($i=0; $i<count($array273)-2; $i++) {
  $j = $i-1;
  $ii = $i+2;
  $iii = $i+3;
  if ($i == 0){
    $array273AbsValue[$i] = $array273[$ii];
  }
  else {
    $array273AbsValue[$i] = $array273AbsValue[$j] - $array273[$iii];
  }
}



jen mě zajímá, jestli není nějaké elegantnější řešení případně, jestli je to správně :-)


Sir Tom:
Díky to vypadá pěkně a je to úspornější.


A navíc v mojem je chyba protože neodečte poslední hodnotu .-(

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm: