Rumah > Artikel > pembangunan bahagian belakang > 新手请问PHP如何循环生成字符串
新手请教PHP怎么循环生成字符串?
如
aa01
aa02
aa03
aa04
......
aa99
我要打印出这样的一组字符串,请问该用什么方式实现?
如果能做到每十组自动分页最好。
------解决方案--------------------
for ($i=1;$i<=99;$i++){<br /> $arr[]=sprintf("aa%02d",$i);<br />}<br />print_r($arr);
<br />[align=left] //字符串二维数组,每是个字符串为一组,也即每十个字符串组成一个数组<br /> $strarr = array();<br /> //计数器,用来计算$strarr内数组元素的个数<br /> $count = 0;<br /> //生成想要的字符串数组<br /> for ( $i = 1; $i <= 99; $i++ ) {<br /> //每生成十个字符串,则计数器$count自加1 <br /> if( ($i - 1 ) % 10 === 0 ) $count++;<br /> //将1~9内的数字转为0i格式的字符串<br /> $stri = ( $i >= 1 && $i <= 9) ? ('0'.$i) : $i;<br /> $strarr[$count - 1][] = 'aa'.$stri;<br /> }<br /> echo '<pre class="brush:php;toolbar:false">';<br /> print_r($strarr);<br /> echo '';[/align]
<br /><?php<br />$arr = array();<br />for($i=1; $i<100; $i++){<br /> $arr[(int)($i-1)/10][] = sprintf("aa%s", str_pad($i,2,'0',STR_PAD_LEFT));<br />}<br />print_r($arr);<br />?><br />
for($i=0; $i<100; $i++) $res[] = sprintf('aa%02d<br>', $i);<br />echo join("<p>\n", array_map('join', array_chunk($res, 10)));
aa00<br>aa01<br>aa02<br>aa03<br>aa04<br>aa05<br>aa06<br>aa07<br>aa08<br>aa09<br><p><br />aa10<br>aa11<br>aa12<br>aa13<br>aa14<br>aa15<br>aa16<br>aa17<br>aa18<br>aa19<br><p><br />aa20<br>aa21<br>aa22<br>aa23<br>aa24<br>aa25<br>aa26<br>aa27<br>aa28<br>aa29<br><p><br />aa30<br>aa31<br>aa32<br>aa33<br>aa34<br>aa35<br>aa36<br>aa37<br>aa38<br>aa39<br><p><br />aa40<br>aa41<br>aa42<br>aa43<br>aa44<br>aa45<br>aa46<br>aa47<br>aa48<br>aa49<br><p><br />aa50<br>aa51<br>aa52<br>aa53<br>aa54<br>aa55<br>aa56<br>aa57<br>aa58<br>aa59<br><p><br />aa60<br>aa61<br>aa62<br>aa63<br>aa64<br>aa65<br>aa66<br>aa67<br>aa68<br>aa69<br><p><br />aa70<br>aa71<br>aa72<br>aa73<br>aa74<br>aa75<br>aa76<br>aa77<br>aa78<br>aa79<br><p><br />aa80<br>aa81<br>aa82<br>aa83<br>aa84<br>aa85<br>aa86<br>aa87<br>aa88<br>aa89<br><p><br />aa90<br>aa91<br>aa92<br>aa93<br>aa94<br>aa95<br>aa96<br>aa97<br>aa98<br>aa99<br><br />