2016-11-22 73 views
1

我有一個表格,名爲$row['seuil_pil'],它只包含數字。我想將這些數字增加一定數量,但不起作用。簡單數學表格PHP

這裏是我的代碼:

$roof=0.12;  

$seuil_haut= array_product(($row['seuil_pil']*$roof) + $row['seuil_pil']); 

OR

$seuil_haut= array(($row['seuil_pil']*$roof) + $row['seuil_pil']); 

OR

$seuil_haut= ($row['seuil_pil']*$roof) + $row['seuil_pil']; 

OR

foreach ($row['seuil_pil'] as $seuil_haut[]) 
{ 
    $seuil_haut[] = ($roof * $row['seuil_pil'] + $row['seuil_pil']); 
} 
+0

你有沒有想過使用foreach循環呢? – rbr94

+0

是的,但我認爲我沒有理解它 – elianero

+0

它是一維數組嗎? '$ row ['seuil_pil']'的內容是怎樣的? – rbr94

回答

1

使用for -loop:

$roof = 0.12; 

$row['seuil_pil'] = array(4,1,3); 

for($i = 0; $i < count($row['seuil_pil']); $i++) { 
    $row['seuil_pil'][$i] *= (1 + $roof); 
} 

var_dump($row['seuil_pil']); 

//results in: 
//array(3) { [0]=> float(4.48) [1]=> float(1.12) [2]=> float(3.36) } 
+0

@downvoter:我編輯了我的帖子。我錯誤地使用了'$ row ['seuil_pil']'而不是'$ entry' – rbr94

+0

這仍然不起作用,因爲您沒有使用引用。計算結果不會在任何地方使用。 –

+0

我的答案將起作用。測試它並測試你的。如果我錯了,請降低我的答案,如果我錯了,請刪除你的答案。 –

3

如果要應用,你遍歷操作數組,你必須使用foreach與參考:

foreach ($row['seuil_pil'] as &$cell) { 
    $cell = $cell * $roof + $cell; 
}