1

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
0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.