Heim >Backend-Entwicklung >PHP-Tutorial >关于php循环输出解决方案
关于php循环输出
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$list="";if(!$car['name']){ $list= '没有任何信息!';}else{ $i=1; foreach($car['name'] as $nm){ $list.=' '.$nm['xm'].' '; $i++; }}print_r($list);
$car['name'] = array( array('xm' => '李强'), array('xm' => '李强'), array('xm' => '李强'), array('xm' => '鸿飞'),);$list="";if(!$car['name']){ $list= '没有任何信息!';}else{ $i=1; $buf = array(); foreach($car['name'] as $nm){ if(! isset($buf[$nm['xm']])) $buf[$nm['xm']] = ''; $list .= "\n" . $nm['xm'] . ($buf[$nm['xm']]++) . "\n"; $i++; }}print_r($list);<br><font color="#e78608">------解决方案--------------------</font><br>
$list=""; $car = array('name'=>array(0=>array('xm'=>'liming'),1=>array('xm'=>'bill'), 2=>array('xm'=>'bill'), 3=>array('xm'=>'bill'))); if(!$car['name']){ $list='Nothing'; }else{ $i = 1; foreach($car['name'] as $nm) { if(strstr($list,$nm['xm']) && $i == 1) { $list = str_replace($nm['xm'],$nm['xm'].$i,$list); $list .= $nm['xm'].($i+1); $i++; }else{ if(strstr($list,$nm['xm'])) { $list .= $nm['xm'].($i+1); $i++; }else{ $list .= $nm['xm']; } } } } print_r($list);<div class="clear"> </div>