0

In php, I have an array that holds a list of arrays that has a list of arrays.

$data = array();
$driveArr = array();

I have a loop that 

array_push($driveArr, array("text" => $text,
                      "spot" => $play,
                      "ball" => $hasball,
                      "togo" => $togo,
                      "type" => $type,
                      "drive"=> $drive)
Then, array_push($data,$driveArr);
$driveArr=array(); //resets $driveArr then go back to the top of the loop

so, $data has bunch of $driveArr

then, i did echo json_encode($data); but it give me blank page.

the overall goal is that i can use this array of array of array in javascript

 var data = JSON.parse("<?php echo json_encode($data) ?>");

and i want to see how it's structured so i can pick out middle group of array at a time and access text, spot,ball,togo, type, drive.

i guess my first question is how is json structure so i can at least debug it.

i know it's

     $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
     echo json_encode($arr);

     = {"a":1,"b":2,"c":3,"d":4,"e":5}

in php, i did

$driveNum = 1;
foreach($data as $x=>$x_value){
foreach($x_value as $y=>$y_value){
    foreach($y_value as $z=>$z_value){
    if ($x == $driveNum){
        echo "Key=" . $z . ", Value=" . $z_value;
        echo "<br>";
    }
    }
}
}

and everything has saved correctly. I want to access it via $driveNum = ; in javascript

so let's say everything worked. i dont know if it did or not. and i have var data in javascript.
how do i use data to pick out an array of arrays and individually access text,spot,ball,togo,type and drive?

15
  • add this before the echo json_encode();: header('Content-Type: application/json'); Commented Mar 18, 2013 at 11:51
  • do a print_r($data) to make sure the array actually holds the data you think it's holding Commented Mar 18, 2013 at 11:55
  • JSON.parse("<?php echo json_encode($data) ?>"); What do you suppose will happen in the extremely likely event that your JSON has double-quotes? Commented Mar 18, 2013 at 11:56
  • wouldn't it work if you just leave the doublequotes? var data = JSON.parse(<?php echo json_encode($data) ?>); Commented Mar 18, 2013 at 11:57
  • 1
    I think you dont have to parse the json twice. So var data = JSON.parse("<?php echo json_encode($data) ?>"); can simply changed to var data = <?php echo json_encode($data) ?>;. Do you get the expected result when using var_dump on $data ? Commented Mar 18, 2013 at 11:58

0

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.