Home >Backend Development >PHP Tutorial >关于php循环输出解决方案

关于php循环输出解决方案

WBOY
WBOYOriginal
2016-06-13 10:34:161184browse

关于php循环输出

PHP code
<!--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);

以上代码中输出数据:李强、李强、李强、鸿飞
是否能实现判断输出的数据中结果中如果“李强”重复,则强制改名为李强1、李强2、李强2、鸿飞?

------解决方案--------------------
计数呀
PHP code
$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>
PHP code
$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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn