A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearHow can I convert all values of an array to floats in PHP? - page 1
User InfoPosts
How can I convert all values of an array to floats in PHP?#1
I am fetching an array of floats from my database but the array I get has converted the values to strings.

How can I convert them into floats again without looping through the array?
Alternatively, how can I fetch the values from the database without converting them to strings?



EDIT:


I am using the Zend Framework and I am using PDO_mysql. The values are stored one per column and that is a requirement so I can't serialize them.
array_map('floatval', $array) only works on single dimensional arrays.
I can't floatval the single elements when I use them because I have to pass an array to my flash chart.
The momentary, non-generic solution is to extract the rows and do array_map('floatval',$array) with each row.

posted date: 2008-12-11 12:10:00


Re: How can I convert all values of an array to floats in PHP?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-11 12:10:01


Re: How can I convert all values of an array to floats in PHP?#3
You could use $floats = array_map('floatval', $nonFloats);There is the option PDO::ATTR_STRINGIFY_FETCHES but from what I remember, MySQL always has it as trueEdit: see Bug 44341 which confirms MySQL doesn't support turning off stringify.Edit: you can also map a custom function like this:function toFloats($array){ return array_map('floatval', $array);}$data = array_map('toFloats', $my2DArray);

posted date: 2008-12-11 12:15:00


Re: How can I convert all values of an array to floats in PHP?#4
How are you getting your data? mysql, mysqli or PDO, some other way or even some other database?you could use array_map with floatval like so:$data = array_map('floatval', $data);but that still executes a loop and i think it assumes you only have one column in your data.you're probably best of casting to float when you use your value, if you have to. php is likely to do a good job of interpreting it right anyway.

posted date: 2008-12-11 12:19:00


Re: How can I convert all values of an array to floats in PHP?#5
correct, it doesn't work for multiple columns

posted date: 2008-12-11 12:35:00


Re: How can I convert all values of an array to floats in PHP?#6
Not sure what you're asking here? You can cast a string to a float, using (float) $string, but since PHP is dynamically typed, that will happen anyway, when needed. There is no reason to do an explicit cast.What are you using floating point values for?

posted date: 2008-12-11 12:48:00


Re: How can I convert all values of an array to floats in PHP?#7
I know I can cast single values. But I have a multi-array of strings which needs to be a multi-array of floats and the casting doesn't happen if I pass the array to open flash chart. I'm using floating point values for drawing flash radar charts. The solution RoBorg posted is perfectly fine!

posted date: 2008-12-11 13:08:00


Re: How can I convert all values of an array to floats in PHP?#8
LOL... are you working on the same project I am tharkun? I just finished (last night) creating something, in a ZF based project, that uses pdo_mysql to retrieve and format data and then output it as xml for use in a flash piece. The values were going in as strings but needed to be floats. Since I'm also the one who wrote the part that gets the data and the one who created the database I just made sure the data was converted to float before it went into the database. I simply cast the values as float as part of some other formatting, for what it is worth.protected function _c2f($input) { $input = (float)$input; $output = round(($input * 1.8) + 32, 2); return $output; }

posted date: 2008-12-11 13:29:00


Re: How can I convert all values of an array to floats in PHP?#9
:) funny conincidence. I can't do that, my values are already stored as foats in the db, but when I retrieve them I get strings. so I have to convert them to floats again before I send them to the flash. which works nicely now.

posted date: 2008-12-11 13:42:00


Re: How can I convert all values of an array to floats in PHP?#10
Do you know why floats are being returned as strings? I know things like simplexml will return strings... how are you getting the data?

posted date: 2008-12-11 15:20:00


Re: How can I convert all values of an array to floats in PHP?#11
As RoBorg says: Edit: see Bug 44341 which confirms MySQL doesn't support turning off stringify.

posted date: 2008-12-11 15:30:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.