RandyMCode
RandyMCode

Reputation: 13

json_encode output formatting for highcharts

I'm using this code to bring a php var into a js function but I can't seem to get the proper formatting (output). I think highcharts needs numbers but it is creating a string even if I use "intval".

series: [{
            name: 'Spyware',
            data: [0, 3, 0, 0, 0]
        }, {
            name: 'Viruses',
            data: [ <?php echo json_encode(intval($virusNum)); ?> , 0, 0, 0, 0] 
        }, {
            name: 'Brute Force Attacks',
            data: [0, 0, 0, 0, 0]
        }, {
            name: 'Host Sweeps',
            data: [0, 0, 0, 0, 0]
        }, {
            name: 'Anonymizer/Proxy Server',
            data: [0, 0, 0, 0, 0]
        }]

Here's where I create the $virusNum variable.

$viruses[$aPos] = intval($freq);
$virusNum = $virusNum + $viruses[$aPos];

Upvotes: 0

Views: 101

Answers (2)

Mike Brant
Mike Brant

Reputation: 71384

Why json-encode there? This is just an integer value, so there is no need for that.

data: [ <?php echo intval($virusNum); ?> , 0, 0, 0, 0] 

Upvotes: 0

jeffjenx
jeffjenx

Reputation: 17467

I think you may need to use the $options parameter of the json_encode function in PHP:

json_encode(intval($virusNum), JSON_NUMERIC_CHECK)

JSON_NUMERIC_CHECK (integer)

Encodes numeric strings as numbers. Available since PHP 5.3.3.

Upvotes: 1

Related Questions