About the PHP loop output solution

Source: Internet
Author: User
About PHP Loop output
PHP Code
  
   $list = "", if (! $car [' name ']) {    $list = ' without any information! ';} else{    $i =1;    foreach ($car [' name '] as $nm) {    $list. = '    . $nm [' XM ']. '    ;  $i + +;    }} Print_r ($list);

The above code output data: Li Qiang, Li Qiang, Li Qiang, Hong Fei
Whether it can achieve the results of the judgment output if "Li Qiang" repeat, then forced renamed to Li Qiang 1, Li Qiang 2, Li Qiang 2, Hong Fei?

------Solution--------------------
Count Yes
PHP Code
$car [' name '] = Array (' xm ' = ' li Qiang '), Array (' xm ' = ' li Qiang '), Array (' xm ' = ' li Qiang '), Array (' xm ' = ' Feihong '),) $list = "", if (! $car [' name ']) {$list = ' no information! ';}    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);
------Solution--------------------
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); 
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.