Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a php array created according to the format I wish to insert into a javascript array variable. My php array values are currently :

Array ( [0] => "Butter noob", 26375 [1] => "Crab Vermecilli", 0 [2] => "Salted Egg Yolk Crab", 0 ) 

Now , i need this array to replace the values within the javascript function code snippet below :

series: [{
    name: 'Delivered amount',
    data: [
        ['Bananas', 8],
        ['Kiwi', 3],
        ['Mixed nuts', 1],
        ['Oranges', 6],
        ['Apples', 8],
        ['Pears', 4],
        ["Clementines", 4],
        ['Reddish (bag)', 1],
        ['Grapes (bunch)', 1]
    ]
}]

So after including my converted php array into the javascript function, it should look something like :

series: [{
    name: 'Delivered amount',
    data: [
        ["Butter Noob", 26375],
        ["Crab Vermecelli", 0],
        ["Salted Egg Yolk Crab", 0],

    ]
}]

Can anyone assist me on this matter? I tried json_encode but im stucked now. How do i use converted php array with json_encode under such situation ?

share|improve this question

2 Answers 2

use JsonEncode

in .php file

  <?php $data=Array ( [0] => "Butter noob", 26375 [1] => "Crab Vermecilli", 0 [2] => "Salted Egg Yolk Crab", 0 ) ;?>
<script>
        series: [{
            name: 'Delivered amount',
            data: JSON.parse(<?php echo(json_encode($data));?>);
        }]
</script>
share|improve this answer
    
there is an error : missing } after property list [Break On This Error] data: JSON.parse(["\"Butter noob\",..., 0","\"Salted Egg Yolk Crab\", 0"]); –  Nicholas Lim 20 hours ago
    
define the $data as $data=Array ( [0] => Array("Butter noob", 26375), [1] => Array("Crab Vermecilli", 0), [2] => Array("Salted Egg Yolk Crab", 0) ) ; –  Patato 20 hours ago
    
the valeus are converted to json but the error is still persisting. Im seeing in Firebug the following code : series: [{ name: 'Delivered amount', data: JSON.parse(["\"Butter noob\", 26375","\"Crab Vermecilli\", 0","\"Salted Egg Yolk Crab\", 0"]); }] –  Nicholas Lim 19 hours ago
    
i copied paste ur code and the following error came out Parse error: syntax error, unexpected '[', expecting ')' in C:\xampp\htdocs\Finalv2\graph.php on line 78 –  Nicholas Lim 19 hours ago

IN PHP file:

$data = json_encode(array(
  array("Butter noob", 26375), 
  array("Crab Vermecilli", 0), 
  array('Salted Egg Yolk Crab', 0)
));

<script>
var series = [{
    name: 'Delivered amount',
    data: JSON.parse( <?php echo $data ?> )
}];
</script>

... chart 
series: series
share|improve this answer
    
doesnt work. The error on firebug shows : SyntaxError: missing } after property list ",26375],["Crab Vermecilli",0],["Salted Egg Yolk Crab",0]] ); –  Nicholas Lim 15 hours ago
    
Remove ";" after JSON.parse( <?php echo $data ?> ); –  Alexander 14 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.