The correct answer is the comment of colleague ValdeirPsr. You're spoiling the array in the loop:
for ($i=0; $i < $retornar ; $i++) {
$ccs = $ccs[$i];
echo $ccs; // Na segunda iteração, a linha anterior vai falhar
}
Just change the name of the variable, or use the index already in echo
:
for ($i=0; $i < $retornar ; $i++) {
$ccs2 = $ccs[$i];
echo $ccs2;
}
Alternative
If it were not an exercise, but an actual application, you could use what is already ready in PHP, in a single line:
print_r( array_slice( $ccs, 0, $_GET['retornar'] );
Or even
$pedaco = array_slice( $ccs, 0, $_GET['retornar'] );
foreach ($pedaco as $key=>$value) {
echo $value;
}
See working at IDEONE
The array_slice
returns an array extracted from another, determined by an offset and a length
Manual:
link