I'm trying to fill a javascript array from a php multidimensional array.
I've used the following code to convert the php array to a javascript array:
var bookings = <?php echo json_encode( $bookeddates ) ?>;
The code that's working:
var unavailableDates = [
{start: bookings[0][1], end: bookings[0][2]},
{start: bookings[1][1], end: bookings[1][2]},
];
The code that's not working:
var unavailableDates = [
for (var i = 0; i < bookings.length; i++) {
{start: bookings[i][1], end: bookings[i][2]},
}
];
The solution is very simple I guess but I'm struggling with this problem for days already. what am I doing wrong?
asked Oct 25, 2016 at 13:56
Mike van den Hoek
211 silver badge9 bronze badges
1 Answer 1
You cannot do a for loop inside an JS array notation - first create the array and then fill it.
var unavailableDates = [];
for (var i = 0; i < bookings.length; i++) {
unavailableDates.push({start: bookings[i][1], end: bookings[i][2]});
}
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/push
answered Oct 25, 2016 at 14:04
andreashager
6573 silver badges9 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default