Jump to content

Recommended Posts

Hello everyone

I have an array ( php ) which i want it to use in javascrip. i have used json encode function but this is not the problem. I want to use the values from an array and plot a graph

 

The PHP array looks like this

        <?php                 
$visits = array(           
            'UK' => 5,
            'US' => 10,
            'AS' => 15,
            'AD' => 20,
            'FG' => 25,
            'HF' => 30,
            'DG' => 35,
            'JH' => 40,
            'ET' => 45,
            'HG' => 50,
            'KT' => 55,
            'ER' => 10,
            'UI' => 65,
            'ER' => 70,
            'UY' => 75,
            'UT' => 80,
            'UK' => 5  
        );
        ?>

and the javascript file is

<script type="text/javascript">
         var arr  = "<?php  json_encode($visits) ?>";
$(function () {
        $('#container').highcharts({
            chart: {
                type: 'area'
            },
            title: {
                text: 'Total Impressions'
            },
            subtitle: {
                text: ''
            },
            xAxis: {
                labels: {
                    formatter: function() {
                        return this.value; // clean, unformatted number for year
                    }
                }
            },
            yAxis: {
                title: {
                    text: 'Hits'
                },
                labels: {
                    formatter: function() {
                        return this.value / 1000 +'k';
                    }
                }
            },
            tooltip: {
                pointFormat: '{series.name} produced <b>{point.y:,.0f}</b><br/>warheads in {point.x}'
            },
            plotOptions: {
                area: {
                    pointStart: 1940,
                    marker: {
                        enabled: false,
                        symbol: 'circle',
                        radius: 2,
                        states: {
                            hover: {
                                enabled: true
                            }
                        }
                    }
                }
            },
            series: [{
                name: 'Country',
                data: [null, null, null, null, null, 6 , 11, 32, 110, 235, 369, 640,    ---  This are all demo values
                    1005, 1436, 2063, 3057, 4618, 6444, 9822, 15468, 20434, 24126,        |
                    27387, 29459, 31056, 31982, 32040, 31233, 29224, 27342, 26662,        |
                    26956, 27912, 28999, 28965, 27826, 25579, 25722, 24826, 24605,        |  <---- This is where i want to loop through the countries from my array
                    24304, 23464, 23708, 24099, 24357, 24237, 24401, 24344, 23586,        |
                    22380, 21004, 17287, 14747, 13076, 12555, 12144, 11009, 10950,        |
                    10871, 10824, 10577, 10527, 10475, 10421, 10358, 10295, 10104 ]     ---
            }, {
                name: 'Hits',
                data: [null, null, null, null, null, null, null , null , null ,null,     ---   This are all demo values
                5, 25, 50, 120, 150, 200, 426, 660, 869, 1060, 1605, 2471, 3322,           |
                4238, 5221, 6129, 7089, 8339, 9399, 10538, 11643, 13092, 14478,            |
                15915, 17385, 19055, 21205, 23044, 25393, 27935, 30062, 32049,             |  <---- This is where i want to loop through the values from my array.
                33952, 35804, 37431, 39197, 45000, 43000, 41000, 39000, 37000,             |
                35000, 33000, 31000, 29000, 27000, 25000, 24000, 23000, 22000,             |
                21000, 20000, 19000, 18000, 18000, 17000, 16000]                         ---
            }]
        });
    });
    

        </script>

Any help will be greatly appreciated...

 

Thank in advance,....

Link to comment
https://forums.phpfreaks.com/topic/287409-cant-use-php-array-in-javascript/
Share on other sites

Your PHP array will be a JavaScript object, so you can use a for (property in object) loop

e.g.

for (p in arr) {
if (arr.hasOwnProperty(p)) {
// do something
}
}
Also, you have to remove the quotes from the "<?php json_encode($visits) ?>"; Edited by nogray
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.