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

in my controller i have an array which i have populated from my database and stored which looks like this and is called dataset2

    array(2) {
      ["April"]=> int(92)
      ["May"]=>  int(86)
    }

In my view i can dd{{}} on the array and see this is the structure.

Now I want to convert it into a javascript array so i can use flot to make it into a graph.

my Javascript within my view looks like this

 <script language="javascript" type="text/javascript">
    $(function () {

        var data1 = [
            ["April", 13],
            ["May", 20],
         ];

   var data2 = [<?php echo json_encode($dataset2 );?>];

   $.plot("#placeholder", [{
       data: data1,
       label: "NewBeach"
   }, {
       data: data2,
       label: "Sandhills"
   }], {
       series: {
           lines: { show: true },
           points: {
              show: true,
              barWidth: 0.1,
              align: "center"
           }
       },
       xaxis: {
           mode: "categories"
       },
        yaxis: {
       },
       grid: { 
           hoverable: true, 
           clickable: true 
       }
   });
});
</script>

Am i missing something when converting it?, as it doesnt draw anything with the JSON_encode array but does with the hard coded one. From what i have read it seems as though thats all i need. Is it because of the values within my array?

Kind regards Mike

share|improve this question
    
you have already done it: var data2 = <?php echo json_encode($dataset2 );?>; just remove[] – Fury May 14 '14 at 13:42
up vote 6 down vote accepted

you have already done it: var data2 = ; just remove the brackets [ ]

also assign key to your array e.g. array('key'=>'value')

$array = array(label=>value, name=>value);

var data2 = <?php echo json_encode($dataset2 );?>;
share|improve this answer
1  
This is correct, $arr = array('name'=>'bart', 'friend'=>'milhouse'); print json_encode($arr); returns {"name":"bart","friend":"mil house"} – dotty May 14 '14 at 13:51
    
Thanks for your help took me a couple of mins to get my head round it now but its clear now, thanks for your time. – Mike May 14 '14 at 14:18
    
glad it worked :) – Fury May 14 '14 at 14:26

As an alternative to the other answers - you could consider using a package for this. Jeffrey Way from Laracasts made this package laracasts/PHP-Vars-To-Js-Transformer

JavaScript::put('data2', $dataset2);
share|improve this answer
    
Thanks for your answer i will have to use this later on for sure. Im probably not using laravel to its full potential. – Mike May 14 '14 at 14:19

Your php array is an associative array, and that will be converted into a javascript object:

array(2) {
  ["April"]=> int(92)
  ["May"]=>  int(86)
}
// Will be:
{
  "April": 92,
  "May": 86
}
// When you `json_encode` it.  

If you wish it to look like your data1 javascript array, it will have to look something like this in php:

$array = Array(
  Array("April", 92),
  Array("May", 86)
);

In the above array, you put the objects in as arrays instead, which will produce an array with arrays (multi dimensional array) for javascript after json_encode.

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.