I want to convert a multi-dimensional PHP array to JavaScript and found this script in a post here;
<script type='text/javascript'>
<?php
$js_array = json_encode($php_array);
echo "var javascript_array = ". $js_array . ";\n";
?>
</script>
But the JS array remains empty. Is it because I have a multi-dimensional array?
Based on the comment you gave to one of the answers, are you sure
$js_arrayis empty or are you just not decoding it? If you look at the generated JavaScript, do you seevar javascript_array = ;or something else? Alternatively you could addalert( javascript_array );to check.EDIT: Since the variable is ok, you can now access the elements with
javascript_array[0].id,javascript_array[0].questionetc.