Skip to content Skip to sidebar Skip to footer

Use Php Array In Javascript?

my php code looks like this: $result['firstName']['lastName']='johan'; echo json_encode($result); how should i type to use this array in javascript with jquery? ...function(data)

Solution 1:

JQuery doesn't effect object access, so you can just do

data.firstName.lastName

Solution 2:

Javascript doesn't technically have associative arrays, so technically in Javascript you're working with an Object. Either syntax you used should work.

Solution 3:

This worked for me but its very ugly

<?php$result['firstName']['lastName']='johan';
$data =  json_encode($result);

?><html><bodyonload='myfunction(<?phpecho$data; ?>);'><script>functionmyfunction(data) 
{
alert(data.firstName.lastName);
}
</script></body></html>

Post a Comment for "Use Php Array In Javascript?"