I like Chad's answer, especially when the query results are passed javascript in the browser. Javascript treats purely numerical objects as numbers, but requires additional work to process numerical objects such as strings. that is, should use parseInt or parseFloat on them.
Based on the Chad solution, I use this, and this is often exactly what I need, and creates structures that can be JSON encoded for the convenience of working with javascript.
while ($row = $result->fetch_assoc()) { // convert numeric looking things to numbers for javascript foreach ($row as &$val) { if (is_numeric($val)) $val = $val + 0; } }
Adding a number string to 0 creates a number type in PHP and correctly identifies the type, so floating point numbers will not be truncated to integers.
d'Artagnan Evergreen Barbosa Feb 17 '15 at 18:59 2015-02-17 18:59
source share