Reputation: 21
I'm having a problem with parsing a 2-dimensional PHP array to Javascript via the json_encode function. This is how I populate my PHP array:
if( $result->num_rows > 0 ) {
$i = 0;
while( $row = $result->fetch_assoc() ) {
$idkaart[$i] = array($row["klant_id"],$row["plaats"],$row["land"],$row["mailing"],$row["korting"],$row["tegoed"],$row["status"],
$row["startdatum"],$row["laatstonline"],$row["kolom"],$row["rij"],$row["fontsize"],$row["overbodenmail"]);
//echo $idkaart[$i];
$i += 1;
}
}
After that I use a simple double for loop to display the array on my screen, to see if it populated correctly.
The weird part is that when I try to load it into a Javascript variable via json_encode: var temp = <?php echo json_encode($idkaart[0]); ?>; alert( temp );
it gives me as response 'null'
When I do an echo json_encode in the PHP part of my code it gives me a valid response.
I have already checked character encodings and such.
I do have been coding for several hours, so I may be making a noob mistake here
Upvotes: 1
Views: 61
Reputation: 21
I got it working thanks to a stupid brain fart I had.
Apparently the Javascript wasn't working because it was above the PHP code generating the array. I've mod the Javascript and now it works without a hassle.
Thanks everyone for trying to think of a solution for this stupid mistake!
Upvotes: 1
Reputation: 14507
2-dimensional array is ok for PHP. You should clarify what's your problem. Is it about PHP's data from MySQL or JS from HTML.
View your html resource to check if the data is there first.
<?php
error_reporting(E_ALL);
$a = array(
array(1,2,3),
array(1,2,4)
);
echo "<script>var a=";
echo json_encode($a[0]);
echo ";alert(a);</script>";
Upvotes: 0