1

In order to draw my chart, I need to have 2 arrays of floats,

  1. The first one stores in MySQL database as a type of string (separated by commas)
  2. And I want to define the second one as a constant array (in PHP)

my problem is how to change the type of data as string(MySQL) and use it as arrays of float in PHP. Also, how should I define the second array? Should I change it to string and then use unserialize?

This just print 1

$data = array();
$data['cols'] = array(
    array('id' => 'Signal','label' => 'Signal', 'type' => 'string'));

$rows = array();
while($r = mysql_fetch_assoc($result)) {
  $temp = array();
  $temp[] = array('v' => $r['Signal']); 
  $res = array_map("floatval", $temp);

  var_dump($res);}                                 

echo var_dump($res);
3
  • 1
    What does your data look like in MySQL. Commented Nov 3, 2014 at 20:11
  • @slapyo, actually these are signals. like this: 0.0, 2.0545915E-5, 4.108994E-5, 6.16302E-5, Commented Nov 3, 2014 at 20:14
  • If you use a 'string' containing a 'floating point representation' in a 'calculation' involving 'floating point' numbers then PHP will convert the 'string' to a 'floating point' number first and then do the calculation accordingly. i.e. '3.14' * 1.0 will result in a floating point value. PHP uses the 'arithmetic context' to 'know' that strings values need to be converted to 'numbers'. i.e. you don't often need to convert explicitly to a 'number' type. Commented Nov 3, 2014 at 22:36

1 Answer 1

1

You can use a combination of floatval() and array_map() to parse your string array into a float array.

A simple tested example:

$input_array = Array("1.1234", "3.123", "2.23E3", "2.23E-3");

$result = array_map("floatval", $input_array);

var_dump($input_array);
var_dump($result);

Prints:

array(4) {
  [0]=> string(6) "1.1234"
  [1]=> string(5) "3.123"
  [2]=> string(6) "2.23E3"
  [3]=> string(7) "2.23E-3"
}
array(4) {
  [0]=> float(1.1234)
  [1]=> float(3.123)
  [2]=> float(2230)
  [3]=> float(0.00223)
}

P.S.: If your database result is somewhat more structured than just a flat array of float-valued strings, you might need to do more than just map it. Provide some more info on what you have and what you expect for more detailed help.


As for your second array, I'm afraid you can't mark arrays as constant. Here is a relevant question. You can go into classes and autosetters, but I'm not sure that kind of complexity is justified for your requirements.

Sign up to request clarification or add additional context in comments.

4 Comments

is it possible to explain more about floatval() and array_map?
@nazanin Certainly. Take a look at the linked docs first and let me know what is confusing. I need to at least see an example of your input data and know more about what you would like to see as a result. The more details - the better. Until then any more details might just go the wrong way and be confusing.
@nazanin Updated with quite a bit more additional research. See if this answers your question and let me know if I can help more.
thank you for the comments, but my data store like ( 0.0, 2.0545915E-5, 4.108994E-5, 6.16302E-5,) without quotations, just its type is string in MySQL. when I use $result = array_map("floatval", $input_array); it print 1 as result. sorry I don't know much in PHP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.