0

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 ?

3 Answers 3

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>
4
  • there is an error : missing } after property list [Break On This Error] data: JSON.parse(["\"Butter noob\",..., 0","\"Salted Egg Yolk Crab\", 0"]); Commented Nov 23, 2014 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) ) ; Commented Nov 23, 2014 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"]); }] Commented Nov 23, 2014 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 Commented Nov 23, 2014 at 8:46
1

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
0
0

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
     }];
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.