Solved

I need to convert a multidimensional array from PHP to javascript

Posted on 2006-07-05
3
892 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
Comment Utility
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
Comment Utility
Perfect!

Thanks for replying so fast!
0
 
LVL 23

Expert Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now