How can I edit a specific line in my csv-file?

my csv looks like this

key1, value1, value2, value3, value4
key2, value5, value6, value7, value8

What I want is to edit the line with the specific key(for example key1). And I want to change value4 by value10.

I was already trying to read the csv with this function:

$a = array();
$f = fopen("gebruikers.csv", 'r');
while ($line = fgetcsv($f, 1000, ","))
$key = array_shift($line);
$a[$key] = $line;

and so I get an array like this:

Array ( [key1] => Array ( [0] => value1 [1] => value2 [2] => value3 [3] => value4) ) this array has the name $a

Next I get the line by $b = $a[key1] and change it by the value $b[3] = $value10.
after this I put it back in the big array $a[key1] = $b.

Now the big array $a is edited correctly, but how do I write it back to my csv-file?

(sorry for my english )