Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

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 Nov 23 '14 at 8:07
    
define the $data as $data=Array ( [0] => Array("Butter noob", 26375), [1] => Array("Crab Vermecilli", 0), [2] => Array("Salted Egg Yolk Crab", 0) ) ; – Patato Nov 23 '14 at 8:10
    
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 Nov 23 '14 at 8:22
    
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 Nov 23 '14 at 8:46

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

in the php use: echo json_encode(yourarray, JSON_NUMERIC_CHECK); Then in javascript use

$.getJSON('urltoyourphp', function(data){
     var series = [{
         name: 'Delivered amount',
         data: data
     }];
});
share|improve this answer

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.