开发者

how to simplify this script or use more advanced built-in function

开发者 https://www.devze.com 2023-03-07 11:14 出处:网络
this is a script which echo formatted value from array . I want to add full stop at the end of array values and put commas in between.

this is a script which echo formatted value from array . I want to add full stop at the end of array values and put commas in between.

This script works but need advanced techniques in php

$array=array("One"=>"Value one",
              "Two"=>"Va开发者_开发百科lue Two",
               "Three"=>"Value Three");

$store ='';     //To store formatted value     
foreach($array as $key=>$val){
if($key=='Three') // Check key value to assign full stop or comma
    $comma=".";
 else
    $comma=",";
$store .=$val.$comma;
}

    echo $store;    //Ans: Value one,Value Two,Value Three.


Using implode will make this so much easier:

echo implode(',', $array) . '.';
0

精彩评论

暂无评论...
验证码 换一张
取 消