SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    multidimensional array

    how would i build a multidimensional array? would it be:

    PHP Code:
    var rinksAndPriceArray = new Array();

        
    rinksAndPriceArray[0] = new Array();
            
    rinksAndPriceArray[1] = new Array(); 
    i've figured that out so far.

    now, i want to be able to access a part of one of the arrays based on a variable that is passed into the function

    the variable i'll send in will be the first part of the array ex: rinksAndPriceArray[0][0] and i'll want to return the rinksAndPriceArray[0][1] value.

    any ideas?
    Last edited by Spidergrant; Sep 12, 2002 at 14:41.

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, you could use a multidimensional array - and then try to remember if myArray[0][1] or myArray[1][0] contains the value you are looking for...

    I vote for this "object oriented" approach:
    Code:
    <html>
      <head>
        <script language="javascript">
          function Item(id, name, price)
          {
            this.id = id;
            this.name = name;
            this.price = price;
          }
          function findItem(arr, id)
          {
            for(var i = 0; i < arr.length; i++)
            {
              if(arr[i].id == id)
              {
                return arr[i];
              }
            }
            return null;
          }
          var arrItems = new Array();
          arrItems[0] = new Item(123, 'A thing', '$99');
          arrItems[1] = new Item(456, 'Another thing', '$55');
          arrItems[2] = new Item(789, 'Stuff', '$1');
          var id = 456;
          var item = findItem(arrItems, id);
          if(item)
          {
            document.write('The name of the item with id ' + id + ' is ' + item.name + 
              ' and the price is ' + item.price);
          }
          else
          {
            document.write('There is no item with id ' + id);
          }
         </script>
      </head>
      <body>
      </body>
    </html>
    Last edited by jofa; Sep 12, 2002 at 15:13.

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahh, that's awesome. i totally see how it works. thanks you very much, sir.

    grant

  4. #4
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    VERY good example Jofa!

    * thinks you should relish the praise - it comes rarely *

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


Bookmarks

Posting Permissions

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