Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

I need to convert a multidimensional array from PHP to javascript

Posted on 2006-07-05
3
Medium Priority
?
918 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

604 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