www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: 2 dimensional arrays

  1. #1
    Join Date
    Sep 2011
    Posts
    8

    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];
    
    ?>
    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>
    What am I doing wrong?

  2. #2
    Join Date
    Nov 2010
    Posts
    1,084
    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>

  3. #3
    Join Date
    Nov 2010
    Posts
    1,084
    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>

  4. #4
    Join Date
    Sep 2011
    Posts
    8
    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];
    }
    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.

  5. #5
    Join Date
    Sep 2011
    Posts
    8
    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];
    }
    }

  6. #6
    Join Date
    Nov 2010
    Posts
    1,084
    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>

  7. #7
    Join Date
    Nov 2010
    Posts
    1,084
    sorry, I didn't see your previous post, and don't really understand what it's trying to do, anyway...

  8. #8
    Join Date
    Sep 2011
    Posts
    8
    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

    PHP Code:
    //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 />";
    }


  9. #9
    Join Date
    Nov 2010
    Posts
    1,084
    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...

  10. #10
    Join Date
    Sep 2011
    Posts
    8
    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.

  11. #11
    Join Date
    Nov 2010
    Posts
    1,084
    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>

  12. #12
    Join Date
    Sep 2011
    Posts
    8
    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 />");
    }
    }
    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.

  13. #13
    Join Date
    Nov 2010
    Posts
    1,084
    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...
    Last edited by xelawho; 09-21-2011 at 04:18 PM.

  14. #14
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,125
    seems kinda hokey to me
    lol.
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  15. #15
    Join Date
    Sep 2011
    Posts
    8
    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
    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
    Last edited by EVENTHORIZON; 09-21-2011 at 04:36 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles