avatar
convert array values to comma-separated string in PHP PHP

• Using the `implode()` function

$temps = array(10, 20, 30);
foreach($arr as $key => $item) {
  $temps[] = $item['attribute'];
}
$str = implode(',', $temps);

• Using the `foreach()` function

$temps = array(10, 20, 30);
$str = '';
foreach ($temps as $temp) {
    $str .= $temp . ',';
}
$str = rtrim($str, ',');

• Using the `array_reduce()` function

$temps = array(10, 20, 30);
$str = array_reduce($temps, function($carry, $item) {
    return $carry . $item . ',';
});
$str = rtrim($str, ',');
You need to login to do this manipulation!