0

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?

5
  • 2
    Please show us a var_dump of your $php_array. Commented Aug 1, 2011 at 7:02
  • Is this dump ok?; array(20) { [0]=> array(6) { ["id"]=> string(3) "106" ["question"]=> string(34) "The capital of Bosnia Herzegovina." ["alternative1"]=> string(8) "Sarajevo" ["alternative2"]=> string(8) "Brussels" ["alternative3"]=> string(5) "Minsk" ["alternative4"]=> string(6) "Vienna" } Commented Aug 1, 2011 at 7:07
  • Double check that $php_array isn't empty. If its not, then $js_array will not be either. :) Commented Aug 1, 2011 at 7:07
  • Which version of PHP? json_encode() is available from 5.2 onward. Do you have error reporting on? Commented Aug 1, 2011 at 7:20
  • The server has 5.2. I don't know if error reporting is on. Commented Aug 1, 2011 at 7:26

4 Answers 4

1

Based on the comment you gave to one of the answers, are you sure $js_array is empty or are you just not decoding it? If you look at the generated JavaScript, do you see var javascript_array = ; or something else? Alternatively you could add alert( javascript_array ); to check.

EDIT: Since the variable is ok, you can now access the elements with javascript_array[0].id, javascript_array[0].question etc.

6
  • In the generated JS is says;var javascript_array = [{"id":"152","question":"The capital of El Salvador.","alternative1":"San Salvador","alternative2":"Roseau","alternative3":"Havana","alternative4":null},{" etc but alert( javascript_array ); gives [object Object], [object Object} etc Commented Aug 1, 2011 at 7:34
  • Looks like you're good to go, then. Is the problem that you don't know how to access the elements in the object? Commented Aug 1, 2011 at 7:38
  • I guess that the problem then might be that I don't know how to access the elements in the object? Can you tell me how? I haven't downvoted anything. Commented Aug 1, 2011 at 7:43
  • @Fred: There is plenty of documentation: developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects Commented Aug 1, 2011 at 7:52
  • @Fred: I edited the answer, but it'd be good to read up on objects as Felix suggested. Commented Aug 1, 2011 at 7:56
0

You can also try something more manual like this:

<?php
// $phpArray= ...
?>
<script type="text/javascript">

  var jsArray = {};
  <?php foreach($phpArray as $key => $val){ ?>
      jsArray.<?php echo $key; ?> = "<?php echo $val; ?>";
  <?php } ?>
  console.log(jsArray);
</script>
-2

If $php_array isn't empty, try to...

var javascript_array = <?php echo $js_array; ?>;

...instead of:

echo "var javascript_array = ". $js_array . ";\n";

Hope it helps! :)

4
  • Still the same problem.. I'm using this to check javascript array; Commented Aug 1, 2011 at 7:18
  • 1) Doesn't respond to his problem and 2) wouldn't make any difference if it did. Commented Aug 1, 2011 at 7:19
  • for(var i=0;i<javascript_array.length;i++){ document.write("<b>javascript_array["+i+"] is </b>=>"+javascript_array[i]+"<br>"); } alert (javascript_array[3][4]); it just says 'jsquestions[0] is =>[object Object]' and 'Undefined' Commented Aug 1, 2011 at 7:20
  • @Fred: So why do you say javascript_array is empty? Obviously javascript_array[0] has a value. You said yourself that it is a multidimensional array (actually it is an array of objects). The array contains one object at index 0. Hence javascript_array[3][4] does not exist. If you, e.g. want the id, you can get it via javascript_array[0].id. Commented Aug 1, 2011 at 7:50
-2

You probably have to eval that json in order for it to be more than just a string in your JavaScript code.

2
  • I'm totally new at this. How do I 'eval' it? Commented Aug 1, 2011 at 7:23
  • There is JavaScript's function eval(), but its not related. Commented Aug 1, 2011 at 7:35

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.