udayan
udayan

Reputation: 11

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

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"=>"Value 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.

Upvotes: 1

Views: 48

Answers (1)

user142162
user142162

Reputation:

Using implode will make this so much easier:

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

Upvotes: 4

Related Questions