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

关于php循环输出解决方案

WBOY
Release: 2016-06-13 13:23:31
Original
1164 people have browsed it

关于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);

Copy after login

以上代码中输出数据:李强、李强、李强、鸿飞
是否能实现判断输出的数据中结果中如果“李强”重复,则强制改名为李强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>
Copy after login
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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template