I've an array in my php called as $tempoHeader
, for example:
Example Array $tempoHeader
-------
Array
(
[0] => red
[1] => green
[2] => yellow
[3] => blue
)
I want to send it into my external javascript, so I do this one in my php code:
My PHP: header.php
$tempoHeader = array_diff_assoc($modelData, $data);
<script type="text/javascript">var header = <?php echo json_encode($tempoHeader); ?>;</script>
<script type="text/javascript" src="upload_header.js"></script>
In my javascript, I've try to console.log(header);
and I got this in my console
Array [ "red", "green", "yellow", "blue" ]
My JS: upload_header.js
$(window).load(function() {
console.log(header); //this work and print out the header json
var myObject = JSON.parse(header);
var myArray = jQuery.parseJSON(header);
console.log(myArray); //didn't print out anything
console.log(myObject); //didn't print out anything
});
How do I can decode this json back as $tempoHeader array form?
Note:
I've read this issue jQuery JSON Decode ( PHP to Javascript), and I've try the suggestion such as jQuery.parseJSON(header);
, JSON.parse(header);
but I got nothing