Solved

I need to convert a multidimensional array from PHP to javascript

Posted on 2006-07-05
3
908 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

726 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