/    Sign up×
Community /Pin to ProfileBookmark

2 dimensional arrays

I have been attempting to learn Javascript, and have minor experience in PHP, I was wondering how to do this code in Javascript:

[CODE]<?php

$personarr=array();

function create_person($firstname,$lastname)
{
global $personarr;
$personarr[]=array($firstname,$lastname);
}

create_person(“John”,”Doe”);
print $personarr[0];

?>[/CODE]

At the moment I have this:

[CODE]<html>
<head>
<script type=”text/javascript”>

var personarr=[];

function createperson(firstname,lastname)
{
personarr[]=[firstname,lastname];
}

createperson(“Djinn”,”Juice”);
document.writeln(personarr[0]);
</script>
</head>
<body>
</body>
</html>[/CODE]

What am I doing wrong?

to post a comment
JavaScript

19 Comments(s)

Copy linkTweet thisAlerts:
@xelawhoSep 21.2011 — if you have no objection to me rewriting your code, this is how I would do it, with comments in code:

[CODE]
<html>
<head>
<script type="text/javascript">
var personarr=[];
function createperson(firstname,lastname) {
//create object with attributes
person = {
giv:firstname,
sur:lastname
}
//push object onto array
personarr.push(person);
//use innerHTML because document write is for suckers
document.getElementById("name").innerHTML=personarr[0].giv+" "+personarr[0].sur;
}
</script>
</head>
<!--body onload function gives page time to load and div to be created before calling function-->
<body onload='createperson("Djinn","Juice")'>
<div id="name"></div>
</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@xelawhoSep 21.2011 — or (closer to what you were doing in the first place):

[CODE]
<html>
<head>
<script type="text/javascript">
var personarr=[];
function createperson(firstname,lastname) {
//create object without attributes
person =firstname+" "+lastname;
//push object onto array
personarr.push(person);
//use innerHTML because document write is for suckers
document.getElementById("name").innerHTML=personarr[0];
}
</script>
</head>
<!--body onload function gives page time to load and div to be created before calling function-->
<body onload='createperson("Djinn","Juice")'>
<div id="name"></div>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@EVENTHORIZONauthorSep 21.2011 — I appreciate the help but the point of an array in an array is to be able to loop through the arrays. I'm not sure using objects would be optimal for what I wish to do.

ie:

[CODE]for (i=0,i<=count(personarr),i++)
{
document.writeln(personarr[i];
}[/CODE]


which I can figure out how to do properly on my own if I can figure out how to add arrays to arrays in the way I have setup.
Copy linkTweet thisAlerts:
@EVENTHORIZONauthorSep 21.2011 — Eventually what I'm going for is going to be like this:

[CODE]for (x=0,x<=count(personarr),x++)
{
for (y=0,y<=count(personarr),y++)
{
document.writeln(personarr[x][y];
}
}[/CODE]
Copy linkTweet thisAlerts:
@xelawhoSep 21.2011 — maybe if you explained a little more what it is you're trying to do?

[CODE]
<html>
<head>
<script type="text/javascript">
var personarr=[];
function createperson(firstname,lastname) {
//create object without attributes
person =firstname+" "+lastname;
//push object onto array
personarr.push(person);
//use innerHTML because document write is for suckers


}

function showNames() {
for (var k=0; k<personarr.length; k++) {
document.getElementById("name").innerHTML+=personarr[k]+"<br>"
}
}

function makeNames() {
createperson("Djinn","Juice");
createperson("Harry","Potter");
createperson("Super","Man");
showNames();
}
</script>
</head>
<!--body onload function gives page time to load and div to be created before calling function-->
<body onload='makeNames()'>
<div id="name"></div>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@xelawhoSep 21.2011 — sorry, I didn't see your previous post, and don't really understand what it's trying to do, anyway...
Copy linkTweet thisAlerts:
@EVENTHORIZONauthorSep 21.2011 — What I'm trying to do is create a function that allows me to insert an array into an array, where the function variables are the ones inserted as an array

in PHP it would be like this

[code=php]//setup array variable
$people_array=array();

//setup function which creates an array within $people_array
function create_people($firstname,$lastname,$state)
{
global $people_array;

$people_array[]=array($firstname,$lastname,$state)
}

//loop through each item in $people_array and print every item one by one
for($x=0;$x<=count($people_array)-1;$x++)
{
for($y=0;$y<=count($people_array[$x])-1;$y++)
{
print $people_array[$x][$y]."<br />";
}
}[/code]
Copy linkTweet thisAlerts:
@xelawhoSep 21.2011 — yeah, I think I see that and it is certainly possible although a little weird. Can I ask why you wouldn't use the first approach (accessing the object's attributes) or the last one (string the name and surname as one attribute and then looping through all of them)?

depending on what you actually want to do, I think a combination of the two would probably be most logical.

to address your second post in this thread, pretty much everything in javascript is an object (even functions can be objects) - it's a little hard to get away from them...
Copy linkTweet thisAlerts:
@EVENTHORIZONauthorSep 21.2011 — The reason I want to do this is to print XML with tags around each attribute such as Firstname, Lastname, City, and to do so I have to do it the way I have for an example.
Copy linkTweet thisAlerts:
@xelawhoSep 21.2011 — like this you mean?

[CODE]
<html>
<head>
<script type="text/javascript">
var personarr=[];
function createperson(firstname,lastname) {
//create object with attributes
person = {
giv:firstname,
sur:lastname
}
//push object onto array
personarr.push(person);

}

function showNames() {
var names = document.getElementById("name");
for (var k=0; k<personarr.length; k++) {
var mydiv=document.createElement('div')
mydiv.appendChild(document.createTextNode('<some tag>'+personarr[k].giv+'</some tag> '+'<some other tag>'+personarr[k].sur+'</some other tag>'));
names.appendChild(mydiv);
}
}

function makeNames() {
createperson("Djinn","Juice");
createperson("Harry","Potter");
createperson("Super","Man");
showNames();
}
</script>
</head>
<!--body onload function gives page time to load and div to be created before calling function-->
<body onload='makeNames()'>
<div id="name"></div>
</body>
</html>

[/CODE]
Copy linkTweet thisAlerts:
@EVENTHORIZONauthorSep 21.2011 — Actually I got it working using this:

[CODE]var personarr=[];

function createperson(firstname,lastname)
{
personarr.push([firstname,lastname]);
}

createperson("Djinn","Juice");

for(x=0;x<=personarr.length;x++)
{
for(y=0;y<=personarr.length;y++)
{
document.writeln(personarr[x][y]+"<br />");
}
}[/CODE]


And then during the for-loop I'll just add a switch statement before and after document.write so it dynamically tags correctly per person.
Copy linkTweet thisAlerts:
@xelawhoSep 21.2011 — mmm... seems kinda hokey to me... have you tried it with an array with more than one object?

and remember: document.write is for suckers... ?
Copy linkTweet thisAlerts:
@cbVisionSep 21.2011 — seems kinda hokey to me[/QUOTE]

lol.
Copy linkTweet thisAlerts:
@EVENTHORIZONauthorSep 21.2011 — For some reason my output seems to be this:

[CODE]<firstname>Djinn</firstname>
<lastname>Juice</lastname>
undefinedundefined
<firstname>Super</firstname>
<lastname>man</lastname>
undefinedundefined
<firstname>Banana</firstname>
<lastname>split</lastname>
undefinedundefined
<firstname>Lady</firstname>
<lastname>Gaga</lastname>
undefinedundefined
[/CODE]


why does it have 2 undefined values between arrays? confusing.

and if I subtract 3 from my personarr.length, it removes the undefined and last 2 actual arrays, banana split and lady gaga
Copy linkTweet thisAlerts:
@xelawhoSep 21.2011 — yes...

might I direct you towards post #11?
Copy linkTweet thisAlerts:
@EVENTHORIZONauthorSep 21.2011 — I fixed my issue, I forgot to include personarr[x] in the second for loop and I only needed < instead of <= for the loop as well.
Copy linkTweet thisAlerts:
@KeverSep 22.2011 — The second loop should be:
for(y=0; y&lt;=personarr[x].length; y++)
Copy linkTweet thisAlerts:
@develmacSep 29.2011 — is my example the correct way to do a 2D array?

var triviaArray = new Array[

["What's the capitol of Canada?","TORONTO","VANCOUVER","OTTAWA","QUEBEC",10,"choice3"]
["How old was Michael Jackson when he died?","35 yrs","50 yrs","40 yrs","47",10,"choice2"]
["What yr was the first Macintosh computer introduced?","1984","1986","2000","1995",10,"choice1"]
["Where is the highest mountain located?","EUROPE","NORTH AMERICA","SOUTH AMERICA","AFRICA",10,"choice4"]
["Who invented the telephone?","Alexander Graham Bell","Albert Einstein","Christopher Columbus","Thomas Edison",10,"choice1"]
["What colours make purple?","green and Cyan","red and green","blue and yellow","red and blue",10,"choice4"]
["How many months have 31 days?","8 MONTHS","7 MONTHS","6 MONTHS","4 MONTHS",10,"choice2"]
["What money do they use in Japan?","POUNDS","EURO","YEN","DOLLARS",10,"choice3"]
["What's the Hungarian word for pepper?","PAPRIKA","PAPPE","POIVRE","EL PIMIENTO",10,"choice1"]
["In which city is Hollywood?","NEW YORK","LOS ANGELAS","MIAMI","ST LOUIS",10,"choice1"]


];
Copy linkTweet thisAlerts:
@justinbarneskinSep 29.2011 — You need the commas-

var triviaArray = [["What's the capitol of Canada?","TORONTO","VANCOUVER","OTTAWA","QUEBEC",10,"choice3"],

["How old was Michael Jackson when he died?","35 yrs","50 yrs","40 yrs","47",10,"choice2"]];
×

Success!

Help @EVENTHORIZON spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...