- I assigned php value into JS variable by jQuery.parseJSON.
- After that, Converting an array-like object(i.e. cars) into a true JavaScript array(i.e. carsArr) by jQuery.makeArray().
- Then I want to get the 1st array(i.e.carsArr[0])of carsArr variable which should be array type as well.
However, the alertbox shows it’string type.
So how can i get the 1st array of carsArr variable? Thanks!
<?php
$car1=array("Saab","Volvo","BMW");
$car2=array("123","234","345");
$cars = array($car1, $car2);
?>
<script type="text/javascript">
$(function () {
var cars =jQuery.parseJSON('<?php echo json_encode($cars); ?>');
var carsArr =jQuery.makeArray(cars);
var aCar = carsArr[0];
alert(typeof(aCar));
});
</script>
Looks like you are doing javascript like variable assignment in PHP
Change
$cars = array(car1, car2);To
$cars = array($car1, $car2);