Solved

I need to convert a multidimensional array from PHP to javascript

Posted on 2006-07-05
3
905 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video Tutorial help 2 50
Hta File displays dynamic File names 4 65
Make New Browser Window Fully Functional 7 34
Error Handler 8 38
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 …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

738 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