我有混合值的數組:PHP:如果號碼(帶逗號),將其轉換爲正確的數字格式(與點)
$row = array('Unspecified risk','Yes','8','3','2','13','none','-1,49','-2,51','-1,46','-1,54'); -1,94 -1,55
正如你可以看到它包含文本和消極和積極逗號 - 值。
我需要將數值轉換爲正確的數字格式,並保持文本值不變。
現在我遍歷值:
foreach ($row as $value) {
// If $value is numeric, convert it to the
// right number format for use with MySQL (decimal(10,2))
// If not, leave it be.
}
兩個相關的問題,我已經調查,但無法找到一個合適的解決方案。
- Converting a number with comma as decimal point to float
- I need php regular expression for numeric value including "-" and ","
誰能提供一個實際的例子?
謝謝回答。但是,我如何區分文本值和數字值? – maartenmachiels 2013-03-12 14:05:24
與is_numeric(),檢查我的編輯:D – aleation 2013-03-12 14:17:37
非常優雅的解決方案!感謝您的努力:它像一個魅力。 – maartenmachiels 2013-03-12 14:39:48