0

I'm quite new to php and don't actually know if this is possible

Im currently outputting JSON code with php using the following code

echo  json_encode($output, JSON_NUMERIC_CHECK);

But what I want to do is have the above data inside a variable.

I tried

$JSONDATAX = json_encode($output, JSON_NUMERIC_CHECK);

But it doesn't seem to like it when I call $JSONDATAX.

The original echo way works completely fine.

edit ........

      $lrs = CDB::ExecuteQuery($sql);

            if($lrs) {
    $jsonData = convert($lrs);
}



function convert($lrs) {

    $intermediate = array();

      while ($vals = CDB::GetAssoc($lrs))  {
        $key = $vals['POS'];
        $x = $vals['CODE'];
        $y = $vals['COUNT'];
        $intermediate[$key][] = array('x' => $x, 'y' => $y);
    }


   $output = array();

    foreach($intermediate as $key => $values) {
        $output[] = array(
            "key" => $key,
            'values' => $values
        );
    }

  $data1 = json_encode($output, JSON_NUMERIC_CHECK);


}










?>
<script>

 var negative_test_data = <?php echo $data1; ?>;

var chart;
nv.addGraph(function() {
    chart = nv.models.multiBarChart()
    .color(d3.scale.category10().range())
      .rotateLabels(0)      //Angle to rotate x-axis labels.
      .transitionDuration(300)
       .showControls(true)   //Allow user to switch between 'Grouped' and 'Stacked' mode.
      .groupSpacing(0.24)    //Distance between each group of bars.

      ;

As you can see, I am using php just after var negative_test_data , but it doesn't produce anything.

10
  • If you do a print_r($output) do you get an array back? Commented Oct 9, 2014 at 14:24
  • Can you show the code when you try to "echo" ? Commented Oct 9, 2014 at 14:28
  • I get an array back with the code above by echoing it, and this works fine with the chart i'm using. but I'm trying to say if I try make it a variable, it doesn't work. Commented Oct 9, 2014 at 14:28
  • 1
    Echoing it? If you echo an array you don't get much back. Commented Oct 9, 2014 at 14:31
  • 1
    I don't think I've explained this very well, I will edit post. Commented Oct 9, 2014 at 14:36

2 Answers 2

1

In your edited example, $data is a local variable inside the convert function, so it cannot be accessed outside that function. the result of json_encode should be returned:

$data1 = json_encode($output, JSON_NUMERIC_CHECK);

should be

return json_encode($output, JSON_NUMERIC_CHECK);

Then, the result of the convert function can be echoed:

var negative_test_data = <?php echo $data1; ?>;

should be

var negative_test_data = <?php echo convert($lrs); ?>;

(There should probably be a an additional if around that whole part, depending on what you want to happen when $lrs does not evaluate to true)

Sign up to request clarification or add additional context in comments.

1 Comment

- Thanks for the answer man, worked a treat! Although I had to do var negative_test_data = <?php echo convert($jsonData); ?>; rather than $lrs
0

This should be all you really need:

$phpvar = json_encode($output);
echo "<script>var negative_test_data = {$phpvar};</script>";

2 Comments

i used, $phpvar = json_encode($output, JSON_NUMERIC_CHECK); and inside my <script> tags <?PHP echo "var negative_test_data = {$phpvar};"; ?> It didn't bring anything back, just blank.
then do a var_dump($phpvar). if it says boolean false, then the json encoding failed, and you need to check json_last_error()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.