How to add PHP array to JS array in Highcharts

前端 未结 5 564
耶瑟儿~
耶瑟儿~ 2020-12-20 09:22

All I want is to be able to add this simple PHP array to display in the X axis. It\'s not wanting to display my content at all and I\'m not understanding why.



        
相关标签:
5条回答
  • 2020-12-20 09:54

    An JSON Object is not the same as an js array. You would need to parse the code first. JSON.parse() or eval() might help parsing.

    js_array = JSON.parse('<?php echo json_encode($array) ?>'); 
    

    as @user1477388 commented correctly

    0 讨论(0)
  • 2020-12-20 09:57

    it worked with me like this:

    for categories :

    categories: [<?php for($i = 0; $i < count($name); $i++){ echo "'".$name[$i]."'"; echo ",";} ?>],
    

    for data:

    data: [<?php echo join($age, ',') ?>]
    

    Note that $name is a string array and $age is an integer array. Also keep in mind that this code worked with me when I imbedded the php,Sql query in the same page.

    0 讨论(0)
  • 2020-12-20 09:58

    We seem to be overcomplicating matters here...

    change this:

               js_array = new Array(<?php echo json_encode($array) ?>);
    
               ...
    
               xAxis: {
                    categories: js_array
                }
    

    to this:

    xAxis: {
       categories: <?php echo json_encode($array); ?>
    }
    
    0 讨论(0)
  • 2020-12-20 09:58

    Try this.

    var js_array = <?php echo json_encode($array); ?>;
    
    0 讨论(0)
  • 2020-12-20 10:00

    Take look at the article http://docs.highcharts.com/#preprocessing-data-from-a-database which describe hwo to use array.

    0 讨论(0)
提交回复
热议问题