将字符串拆分为自定义格式

Split string into custom format

如何将此字符串拆分为自定义格式?

输入字符串:

14157.72,39140.94,36383.66,38508.00,8424.00

预期输出:

['14157.72'],['39140.94'],['36383.66'],['38508.00'],['8424.00']

当前代码:

foreach($amount[0] as $aountlist){ 
    //$aountlist.= "'".$aountlist->HPIAmount.',';
    print_r($aountlist->HPIAmount);

    $defineamount = [$aountlist->HPIAmount];
} 

如何更改我的代码以获得预期的输出?

试试 -

$string = '14157.72,39140.94,36383.66,38508.00,8424.00';
$temp = array();
$str = explode(',', $string);
foreach($str as $val){
   $temp[] = "[".$val."]";
}
echo implode(',', $temp);

这应该适合你:

explode() your string by a comma, then simply implode()再来一遍。

$str = "14157.72,39140.94,36383.66,38508.00,8424.00";
echo "['" . implode("'],['", explode(",", $str)) . "']";

输出:

['14157.72'],['39140.94'],['36383.66'],['38508.00'],['8424.00']
$string = "14157.72,39140.94,36383.66,38508.00,8424.00"
$result = preg_replace('/([\d\.]+)/m', '[\'\']', $string );

echo $result;

输出:

['14157.72'],['39140.94'],['36383.66'],['38508.00'],['8424.00']