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?
echo json_encode();
:header('Content-Type: application/json');
JSON.parse("<?php echo json_encode($data) ?>");
What do you suppose will happen in the extremely likely event that your JSON has double-quotes?var data = JSON.parse(<?php echo json_encode($data) ?>);
var data = JSON.parse("<?php echo json_encode($data) ?>");
can simply changed tovar data = <?php echo json_encode($data) ?>;
. Do you get the expected result when usingvar_dump
on $data ?