如何将此字符串拆分为自定义格式?
输入字符串:
foreach($amount[0] as $aountlist){
//$aountlist.= "'".$aountlist->HPIAmount.',';
print_r($aountlist->HPIAmount);
$defineamount = [$aountlist->HPIAmount];
}
预期产出:
event_id, horse_id, place, money
当前代码:
ENTRY (TABLE NAME)
COLUMNS IN TABLE ENTRY:
Event_id,
Horse_id
Place
PRIZE (TABLE NAME)
COLUMNS IN TABLE PRIZE:
Event_id,
Place,
Money
如何更改代码以获得预期的输出?
答案 0 :(得分:2)
尝试 -
$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);
答案 1 :(得分:2)
这应该适合你:
首先用逗号表示explode()
字符串,然后再次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']
答案 2 :(得分:2)
$string = "14157.72,39140.94,36383.66,38508.00,8424.00"
$result = preg_replace('/([\d\.]+)/m', '[\'$1\']', $string );
echo $result;
输出:
['14157.72'],['39140.94'],['36383.66'],['38508.00'],['8424.00']