You'll have to loop on a set of three rows and three columns at a time and shift $i and $j by the starting value of the set:
// sets of three for $i
for($iset=0; $iset<3; $iset++) {
$istart=$iset*3;
for($i=0;$i<3;$i++){
// sets of three for $j
for($jset=0; $jset<3; $jset++) {
$jstart=$jset*3;
for($j=0;$j<3;$j++){
$tab[$istart+$i][$jstart+$j]=$val;
}
}
}
}
So for sets (0, 1, 2) you'll have starting values (0, 3, 6). So you'll end up iterating through like this (which hopefully solves your problem):
i-> (0, 1, 2) j -> (0, 1, 2), (3, 4, 5), (6, 7, 8)
i-> (3, 4, 5) j -> (0, 1, 2), (3, 4, 5), (6, 7, 8)
i-> (6, 7, 8) j -> (0, 1, 2), (3, 4, 5), (6, 7, 8)