i getting random number user input (lets $n). possible create numeric array keys array(1, 2, 3, ....., $n ) number given user? want use in foreach loop , echo value.
here 1 of codes have used.
$n = $_get['num']; foreach ($n $a) { echo $a; } how do that? in advance.
in foreach temporary memory of variable created gets flush after loop has completed it's execution. prefer use foreach when know key | value relationship exist.
create array upto $n, can use for loop:
$array_up_to_n = array(); ($i=1; $i<=$n; $i++) { $array_up_to_n[] = $i; } //you can verify by: print_r($array_up_to_n); i hope want result , have understood correctly
information:
whenever use foreach , want result in array, declare array before foreach , use inside like:
$my_array = array(); foreach ($result $row) { $my_array[] = $row; } because $rowmemory gets flushed after loop
Comments
Post a Comment