I have an array like below;
$carsarray = array("audi","toyota","ford");
I want to generate a string like;
'audi','toyota','ford'
Using implode function like;
$cars = implode(",",$carsarray);
I get output like
audi,toyota,ford
To get my desired output, I use;
$cars = "";
foreach($carsarray as $car){
$cars .= "'".$car."',";
}
$cars = rtrim($cars,","); // this gives me 'audi','toyota','ford'
But, is there any other better / efficient method other than using these foreach
or while
or some other loops? I mean, something like an implode function?