I am trying to print johan using javascript alert box with javascript variable and php array. But following code is not working.
$result['firstName']['lastName']='johan';
$data = json_encode($result);
?>
<html>
<body onload='myfunction(<?php echo $data; ?>);'>
<script>
function myfunction(data)
{
var fn = "firstName";
alert(data.+fn+.lastName);
}
</script>
</body>
</html>
This should do it:
Because your index is inside a variable, you have to use the brackets.