elianero
elianero

Reputation: 27

Simple Math with tables PHP

I have a table called $row['seuil_pil'] that contains only numbers. And i want to increse these numbers by a certain amount but it doesn't work.

Here's my code:

$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'] ); 
}

Upvotes: 1

Views: 79

Answers (2)

rbr94
rbr94

Reputation: 2287

Use a 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) } 

Upvotes: 1

Bartosz Zasada
Bartosz Zasada

Reputation: 3900

If you want to apply operations to the array that you're iterating over, you must use foreach with a reference:

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

Upvotes: 3

Related Questions