I have a php array that I json_encode and pass to a variable in a javascript function. Everything looks ok, and after Json encoding, the array looks like a valid Javascript array, but I keep getting undefined on output. Here's how the code looks like:
<script src="js/jquery-1.8.1.min.js"></script>
<script>
$(function() {
getPhotos();
});
function getPhotos() {
var photosArray= <?php echo json_encode($photos_array); ?>;
alert(photosArray.length);
}
And here is what it looks like in the browser:
function getPhotos() {
var photosArray= {"12":"1401880321163321491.png","11":"1401880200994911748.jpg","10":"1401866598206920573.png","6":"1401863850983364637.png","7":"14018645731921210607.png","8":"14018646851258457972.png","9":"14018647281804140101.png","13":"1401880485229593545.png","15":"1401933387756388853.jpg","16":"14019688951333867090.jpg","17":"14021062501374960169.jpg","18":"14021064801222019341.jpg","19":"14021065441073158757.png","20":"14021066841708486619.png","21":"1402106744569372168.png","22":"14022970452004604041.jpg","23":"1402297585941206466.jpg"};
alert(photosArray.length);
}
Am I doing anything wrong?