subhomoy Posted March 31, 2014 Share Posted March 31, 2014 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 More sharing options...
mac_gyver Posted March 31, 2014 Share Posted March 31, 2014 json_encode() doesn't output anything. you would need to echo the output in order to send it to the browser. Link to comment https://forums.phpfreaks.com/topic/287409-cant-use-php-array-in-javascript/#findComment-1474498 Share on other sites More sharing options...
subhomoy Posted March 31, 2014 Author Share Posted March 31, 2014 ok noted the above mistake but how to loop through the values??? Link to comment https://forums.phpfreaks.com/topic/287409-cant-use-php-array-in-javascript/#findComment-1474504 Share on other sites More sharing options...
nogray Posted March 31, 2014 Share Posted March 31, 2014 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) ?>"; Link to comment https://forums.phpfreaks.com/topic/287409-cant-use-php-array-in-javascript/#findComment-1474554 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.