Stju
17 Mar 2010, 2:27 AM
Maybe someone of You are not aware that MySql returns all data as strings..
JavaScript expects to see boolean values as boolean and numbers as numbers, otherwise this can led to some unpredictable problems.
This is simple solution to solve typecasting problem :">
To illustrate (without use of fix_cast) json_encode result:
[{"number":"5","boolean_value":"true"}]
Using fix_cast:
[{"number":5,"boolean_value":true}]
Here comes the medicine:
function fix_cast(&$value, $key){
if($value=='false' || $value=='true'){$value = $value=='true' ? true : false;} //fix boolean values!
if(is_numeric($value))$value= (int)$value; //fix numeric values!!
}
array_walk_recursive($store_data, 'fix_cast');
Hopefully this will save some headache..
Use this straight after MySql request applying to resulting data-set.
Stju
JavaScript expects to see boolean values as boolean and numbers as numbers, otherwise this can led to some unpredictable problems.
This is simple solution to solve typecasting problem :">
To illustrate (without use of fix_cast) json_encode result:
[{"number":"5","boolean_value":"true"}]
Using fix_cast:
[{"number":5,"boolean_value":true}]
Here comes the medicine:
function fix_cast(&$value, $key){
if($value=='false' || $value=='true'){$value = $value=='true' ? true : false;} //fix boolean values!
if(is_numeric($value))$value= (int)$value; //fix numeric values!!
}
array_walk_recursive($store_data, 'fix_cast');
Hopefully this will save some headache..
Use this straight after MySql request applying to resulting data-set.
Stju