1

I have an array generated with the following code where $result is a collection with Salesman a string field and col1 an integer field

        $data=[];

        $arr = ($result->get());
        $j=0;
        foreach($arr as $res)
        {
            $data[$j] = [$res->shortName,$res->col1];
            $j++;
        }

And when i pass this array to Google Charts using json_encode like

 var data = google.visualization.arrayToDataTable({!!  json_encode($data)  !!}

it is rendered in Javascript as:

 var data = google.visualization.arrayToDataTable([["John","700000"],["Jessa","1400000"],["Mercy ","1100000"],["William","780000"],["Thomas","550000"]]
                  );

As you can see the integer numbers are also rendered as strings with a quote. Is there any way to avoid this ?

2
  • 1
    Cast to int : (int)$res->col1 Commented Dec 5, 2015 at 10:56
  • How do you know those fields are integers, not strings? Commented Dec 5, 2015 at 10:58

1 Answer 1

1

I think that you only need to convert the type of your values in PHP, like this:

$data[$j] = [$res->shortName, intval( $res->col1 ) ];   

This way all your $res->col1 values will be stored as int. If you want float values just use floatval instead of intval, like this:

$data[$j] = [$res->shortName, floatval( $res->col1 ) ];
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.