Solved

I need to convert a multidimensional array from PHP to javascript

Posted on 2006-07-05
3
903 Views
Last Modified: 2008-01-09
Hi all, I need to create javascript code that holds all the weekends in two years. I am generating this code with PHP (I don't have a problem with that part). So far I have succesfully created this:

// january = 0, february = 1, etc...
var weekends = {
0 : [1, 7, 8, 14, 15, 21, 22, 28, 29],
1 : [4, 5, 11, 12, 18, 19, 25, 26],
2 : [4, 5, 11, 12, 18, 19, 25, 26],
3 : [1, 2, 8, 9, 15, 16, 22, 23, 29, 30],
4 : [6, 7, 13, 14, 20, 21, 27, 28],
5 : [3, 4, 10, 11, 17, 18, 24, 25],
6 : [1, 2, 8, 9, 15, 16, 22, 23, 29, 30],
7 : [5, 6, 12, 13, 19, 20, 26, 27],
8 : [2, 3, 9, 10, 16, 17, 23, 24, 30],
9 : [1, 7, 8, 14, 15, 21, 22, 28, 29],
10 : [4, 5, 11, 12, 18, 19, 25, 26],
11 : [2, 3, 9, 10, 16, 17, 23, 24, 30, 31]
};

But this holds the weekends for only one year. So now I need to add the third dimension to this array -which would be the year-.

In PHP this would look something like this:

$weekends = array(
    2006 => array (
        1 => array (1, 2, 3, 4),
        2 => array (1, 2, 3, 4),
        3 => array (1, 2, 3, 4),
        4 => array (1, 2, 3, 4)
        ),
    2007 => array (
        1 => array (1, 2, 3, 4),
        2 => array (1, 2, 3, 4),
        3 => array (1, 2, 3, 4),
        4 => array (1, 2, 3, 4)
        )
);

How can I turn this into javascript?
0
Comment
Question by:poisa
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 500 total points
ID: 17045908
Try

var weekends = {
2001: {
0 : [1, 7, 8, 14, 15, 21, 22, 28, 29],
1 : [4, 5, 11, 12, 18, 19, 25, 26],
2 : [4, 5, 11, 12, 18, 19, 25, 26],
3 : [1, 2, 8, 9, 15, 16, 22, 23, 29, 30],
4 : [6, 7, 13, 14, 20, 21, 27, 28],
5 : [3, 4, 10, 11, 17, 18, 24, 25],
6 : [1, 2, 8, 9, 15, 16, 22, 23, 29, 30],
7 : [5, 6, 12, 13, 19, 20, 26, 27],
8 : [2, 3, 9, 10, 16, 17, 23, 24, 30],
9 : [1, 7, 8, 14, 15, 21, 22, 28, 29],
10 : [4, 5, 11, 12, 18, 19, 25, 26],
11 : [2, 3, 9, 10, 16, 17, 23, 24, 30, 31]
},
2002: {
0 : [1, 7, 8, 14, 15, 21, 22, 28, 29],
1 : [4, 5, 11, 12, 18, 19, 25, 26],
2 : [4, 5, 11, 12, 18, 19, 25, 26],
3 : [1, 2, 8, 9, 15, 16, 22, 23, 29, 30],
4 : [6, 7, 13, 14, 20, 21, 27, 28],
5 : [3, 4, 10, 11, 17, 18, 24, 25],
6 : [1, 2, 8, 9, 15, 16, 22, 23, 29, 30],
7 : [5, 6, 12, 13, 19, 20, 26, 27],
8 : [2, 3, 9, 10, 16, 17, 23, 24, 30],
9 : [1, 7, 8, 14, 15, 21, 22, 28, 29],
10 : [4, 5, 11, 12, 18, 19, 25, 26],
11 : [2, 3, 9, 10, 16, 17, 23, 24, 30, 31]
}
};

0
 
LVL 2

Author Comment

by:poisa
ID: 17045954
Perfect!

Thanks for replying so fast!
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 17046020
Thanks, you did most of the work FOR me by already knowing the 2 dimensional case!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert Silverlight ERP To Angularjs,HTML5 3 56
HTML auto refresh to another site 4 28
javascript form submition 2 21
Aspx calendar pop up control 3 27
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question