How can I read array values without using key/index value?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randell D.

    How can I read array values without using key/index value?


    Folks,

    Here's my problem:

    I am createing an array in a file using the reslut of some PHP and MySQL
    processing. An example of my array would be

    examlpe['a']="example one";
    examlpe['b']="example two";
    examlpe['c']="example three";
    examlpe['d']="example four";
    examlpe['e']="example five";
    examlpe['f']="example six";

    The array key (or index) can have different values (always unique though).

    I want to be able to reference/read, for example, the second and fourth
    element in the array - but since my array keys/indexes are dynamic, my
    standard script will only know the array is called 'example'...

    So... using the example above, I would not know that the first element in
    the array is element['a'].

    My Question: Is there a method that I can use to reference any part of the
    array, without knowing the elements key?

    If not, is there a method to convert the array to have numeric keys?

    Thanks,
    Randell D.



  • Douglas Crockford

    #2
    Re: How can I read array values without using key/index value?

    > I am createing an array in a file using the reslut of some PHP and MySQL[color=blue]
    > processing. An example of my array would be
    >
    > examlpe['a']="example one";
    > examlpe['b']="example two";
    > examlpe['c']="example three";
    > examlpe['d']="example four";
    > examlpe['e']="example five";
    > examlpe['f']="example six";
    >
    > The array key (or index) can have different values (always unique though).
    >
    > I want to be able to reference/read, for example, the second and fourth
    > element in the array - but since my array keys/indexes are dynamic, my
    > standard script will only know the array is called 'example'...
    >
    > So... using the example above, I would not know that the first element in
    > the array is element['a'].
    >
    > My Question: Is there a method that I can use to reference any part of the
    > array, without knowing the elements key?
    >
    > If not, is there a method to convert the array to have numeric keys?[/color]

    Buy a book, man. You are dealing with several misunderstandin gs. Some people
    enjoy being willfully ignorant. I sense that you are not one of those people.

    In JavaScript, a collection of named values is called an object. The values are
    unordered. You can use a for..in statement to retrieve the values and ignore the
    odd ones, but the order is not guarenteed to be stable.

    JavaScript has an array type which takes integer subscripts. This is probably
    what you want. If so, where did you get the nutty idea that you had to use
    letters as subscripts?



    Comment

    • Lee

      #3
      Re: How can I read array values without using key/index value?

      Randell D. said:[color=blue]
      >
      >
      >Folks,
      >
      >Here's my problem:
      >
      >I am createing an array in a file using the reslut of some PHP and MySQL
      >processing. An example of my array would be
      >
      >examlpe['a']="example one";
      >examlpe['b']="example two";
      >examlpe['c']="example three";
      >examlpe['d']="example four";
      >examlpe['e']="example five";
      >examlpe['f']="example six";
      >
      >The array key (or index) can have different values (always unique though).
      >
      >I want to be able to reference/read, for example, the second and fourth
      >element in the array - but since my array keys/indexes are dynamic, my
      >standard script will only know the array is called 'example'...
      >
      >So... using the example above, I would not know that the first element in
      >the array is element['a'].
      >
      >My Question: Is there a method that I can use to reference any part of the
      >array, without knowing the elements key?
      >
      >If not, is there a method to convert the array to have numeric keys?[/color]

      The obvious solution seems to be to change your PHP code to create it
      the way that you want to be able to access it:

      example[0]="example one";
      example[1]="example two";
      example[2]="example three";
      example[3]="example four";
      example[4]="example five";
      example[5]="example six";

      or:

      example[example.length]="example one";
      example[example.length]="example two";
      example[example.length]="example three";
      example[example.length]="example four";
      example[example.length]="example five";
      example[example.length]="example six";

      Comment

      • Randell D.

        #4
        Re: How can I read array values without using key/index value?


        "Douglas Crockford" <nospam@covad.n et> wrote in message
        news:27933$3fd8 e901$44a4ae11$4 578@msgid.megan ewsservers.com. ..[color=blue][color=green]
        > > I am createing an array in a file using the reslut of some PHP and MySQL
        > > processing. An example of my array would be
        > >
        > > examlpe['a']="example one";
        > > examlpe['b']="example two";
        > > examlpe['c']="example three";
        > > examlpe['d']="example four";
        > > examlpe['e']="example five";
        > > examlpe['f']="example six";
        > >
        > > The array key (or index) can have different values (always unique[/color][/color]
        though).[color=blue][color=green]
        > >
        > > I want to be able to reference/read, for example, the second and fourth
        > > element in the array - but since my array keys/indexes are dynamic, my
        > > standard script will only know the array is called 'example'...
        > >
        > > So... using the example above, I would not know that the first element[/color][/color]
        in[color=blue][color=green]
        > > the array is element['a'].
        > >
        > > My Question: Is there a method that I can use to reference any part of[/color][/color]
        the[color=blue][color=green]
        > > array, without knowing the elements key?
        > >
        > > If not, is there a method to convert the array to have numeric keys?[/color]
        >
        > Buy a book, man. You are dealing with several misunderstandin gs. Some[/color]
        people[color=blue]
        > enjoy being willfully ignorant. I sense that you are not one of those[/color]
        people.[color=blue]
        >
        > In JavaScript, a collection of named values is called an object. The[/color]
        values are[color=blue]
        > unordered. You can use a for..in statement to retrieve the values and[/color]
        ignore the[color=blue]
        > odd ones, but the order is not guarenteed to be stable.
        >
        > JavaScript has an array type which takes integer subscripts. This is[/color]
        probably[color=blue]
        > what you want. If so, where did you get the nutty idea that you had to use
        > letters as subscripts?
        >
        > http://www.crockford.com/javascript/javascript.html
        >[/color]

        I'm broke - no work and little savings to carry me forward until a work
        permit gets approved... Secondly, I have put a javascript book on my xmas
        wish list... I'm reliant on old javascript books and online guides that I
        read. I guess my nutty idea of using letters as subscripts might have
        originated from PHP but, while I can't recall where, I thought I'd seen an
        examlpe of someone who had done such (ie used letters (and not variables) to
        identify elements in an array.

        Anyway... I'll work with the for...in loop and see how that helps me... I
        can't have PHP pre-assign the numeric subscript portion of the array as
        several javascript files (containing arrays) are created in advance and web
        pages include as many, or as few of them as are needed... I want to make
        sure there are no duplicates over-write preexisting array elements....

        Thanks for the help though...


        Comment

        • Randell D.

          #5
          Re: How can I read array values without using key/index value?


          "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
          news:brapkm01fs q@drn.newsguy.c om...[color=blue]
          > Randell D. said:[color=green]
          > >
          > >
          > >Folks,
          > >
          > >Here's my problem:
          > >
          > >I am createing an array in a file using the reslut of some PHP and MySQL
          > >processing. An example of my array would be
          > >
          > >examlpe['a']="example one";
          > >examlpe['b']="example two";
          > >examlpe['c']="example three";
          > >examlpe['d']="example four";
          > >examlpe['e']="example five";
          > >examlpe['f']="example six";
          > >
          > >The array key (or index) can have different values (always unique[/color][/color]
          though).[color=blue][color=green]
          > >
          > >I want to be able to reference/read, for example, the second and fourth
          > >element in the array - but since my array keys/indexes are dynamic, my
          > >standard script will only know the array is called 'example'...
          > >
          > >So... using the example above, I would not know that the first element in
          > >the array is element['a'].
          > >
          > >My Question: Is there a method that I can use to reference any part of[/color][/color]
          the[color=blue][color=green]
          > >array, without knowing the elements key?
          > >
          > >If not, is there a method to convert the array to have numeric keys?[/color]
          >
          > The obvious solution seems to be to change your PHP code to create it
          > the way that you want to be able to access it:
          >[/color]

          I can't have PHP pre-assign the numeric subscript portion (what I call
          element keys or indexes) of the array as several javascript files
          (containing arrays) are created in advance and web pages include as many, or
          as few of them as are needed... I want to make sure there are no duplicate
          array elements which might over write an array element that might be
          included from anothre file.

          Thanks though...


          Comment

          • Eric Bohlman

            #6
            Re: How can I read array values without using key/index value?

            "Randell D." <reply.to.news. group.only@and. share.com> wrote in
            news:JIpCb.6764 73$6C4.175820@p d7tw1no:
            [color=blue]
            > I'm broke - no work and little savings to carry me forward until a
            > work permit gets approved... Secondly, I have put a javascript book on
            > my xmas wish list... I'm reliant on old javascript books and online
            > guides that I read. I guess my nutty idea of using letters as[/color]

            Go over to <http://devedge.netscap e.com/library/manuals/> and download the
            "JavaScript 1.5 Core Guide" and "JavaScript 1.5 Core Reference."

            Comment

            • Randell D.

              #7
              Re: How can I read array values without using key/index value?


              "Eric Bohlman" <ebohlman@earth link.net> wrote in message
              news:Xns944F994 D57525ebohlmano msdevcom@130.13 3.1.4...[color=blue]
              > "Randell D." <reply.to.news. group.only@and. share.com> wrote in
              > news:JIpCb.6764 73$6C4.175820@p d7tw1no:
              >[color=green]
              > > I'm broke - no work and little savings to carry me forward until a
              > > work permit gets approved... Secondly, I have put a javascript book on
              > > my xmas wish list... I'm reliant on old javascript books and online
              > > guides that I read. I guess my nutty idea of using letters as[/color]
              >
              > Go over to <http://devedge.netscap e.com/library/manuals/> and download the
              > "JavaScript 1.5 Core Guide" and "JavaScript 1.5 Core Reference."[/color]

              Thanks...


              Comment

              • Lee

                #8
                Re: How can I read array values without using key/index value?

                Randell D. said:[color=blue]
                >
                >
                >"Lee" <REM0VElbspamtr ap@cox.net> wrote in message
                >news:brapkm01f sq@drn.newsguy. com...[color=green]
                >> Randell D. said:[color=darkred]
                >> >
                >> >
                >> >Folks,
                >> >
                >> >Here's my problem:
                >> >
                >> >I am createing an array in a file using the reslut of some PHP and MySQL
                >> >processing. An example of my array would be
                >> >
                >> >examlpe['a']="example one";
                >> >examlpe['b']="example two";
                >> >examlpe['c']="example three";
                >> >examlpe['d']="example four";
                >> >examlpe['e']="example five";
                >> >examlpe['f']="example six";
                >> >
                >> >The array key (or index) can have different values (always unique[/color][/color]
                >though).[color=green][color=darkred]
                >> >
                >> >I want to be able to reference/read, for example, the second and fourth
                >> >element in the array - but since my array keys/indexes are dynamic, my
                >> >standard script will only know the array is called 'example'...
                >> >
                >> >So... using the example above, I would not know that the first element in
                >> >the array is element['a'].
                >> >
                >> >My Question: Is there a method that I can use to reference any part of[/color][/color]
                >the[color=green][color=darkred]
                >> >array, without knowing the elements key?
                >> >
                >> >If not, is there a method to convert the array to have numeric keys?[/color]
                >>
                >> The obvious solution seems to be to change your PHP code to create it
                >> the way that you want to be able to access it:
                >>[/color]
                >
                >I can't have PHP pre-assign the numeric subscript portion (what I call
                >element keys or indexes) of the array as several javascript files
                >(containing arrays) are created in advance and web pages include as many, or
                >as few of them as are needed... I want to make sure there are no duplicate
                >array elements which might over write an array element that might be
                >included from anothre file.[/color]

                If your PHP code creates them in this form:
                example[example.length]="example one";
                example[example.length]="example two";
                example[example.length]="example three";
                example[example.length]="example four";
                example[example.length]="example five";
                example[example.length]="example six";

                Then each one will absolutely have a unique number, representing
                the order in which it was loaded, regardless of how many different
                files they come from.

                Comment

                • Douglas Crockford

                  #9
                  Re: How can I read array values without using key/index value?

                  > > Buy a book, man. You are dealing with several misunderstandin gs.[color=blue][color=green]
                  > > Some people enjoy being willfully ignorant.
                  > > I sense that you are not one of those people.[/color][/color]
                  [color=blue]
                  > I'm broke - no work and little savings to carry me forward until a work
                  > permit gets approved... Secondly, I have put a javascript book on my xmas
                  > wish list... I'm reliant on old javascript books and online guides that I
                  > read. I guess my nutty idea of using letters as subscripts might have
                  > originated from PHP but, while I can't recall where, I thought I'd seen an
                  > examlpe of someone who had done such (ie used letters (and not variables) to
                  > identify elements in an array.[/color]

                  You are breaking my heart. That is about the saddest story I have ever seen in
                  this newsgroup.

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: How can I read array values without using key/index value?

                    Lee wrote:
                    [color=blue]
                    > example[0]="example one";
                    > example[1]="example two";
                    > example[2]="example three";
                    > example[3]="example four";
                    > example[4]="example five";
                    > example[5]="example six";
                    >
                    > or:
                    >
                    > example[example.length]="example one";
                    > example[example.length]="example two";
                    > example[example.length]="example three";
                    > example[example.length]="example four";
                    > example[example.length]="example five";
                    > example[example.length]="example six";[/color]

                    Oh my. Why not

                    var example =
                    ["example one",
                    "example two",
                    "example three",
                    "example four",
                    "example five",
                    "example six"];

                    ?


                    PointedEars

                    Comment

                    • Grant Wagner

                      #11
                      Re: How can I read array values without using key/index value?

                      Thomas 'PointedEars' Lahn wrote:
                      [color=blue]
                      > Lee wrote:
                      >[color=green]
                      > > example[0]="example one";
                      > > example[1]="example two";
                      > > example[2]="example three";
                      > > example[3]="example four";
                      > > example[4]="example five";
                      > > example[5]="example six";
                      > >
                      > > or:
                      > >
                      > > example[example.length]="example one";
                      > > example[example.length]="example two";
                      > > example[example.length]="example three";
                      > > example[example.length]="example four";
                      > > example[example.length]="example five";
                      > > example[example.length]="example six";[/color]
                      >
                      > Oh my. Why not
                      >
                      > var example =
                      > ["example one",
                      > "example two",
                      > "example three",
                      > "example four",
                      > "example five",
                      > "example six"];
                      >
                      > ?
                      >
                      > PointedEars[/color]

                      Or, if you add elements in several "blocks" and can't always
                      create a new Array:

                      var a = new Array(); // [];
                      a.push(
                      "example one",
                      "example two",
                      "example three",
                      "example four",
                      "example five",
                      "example six"
                      );
                      // more code
                      a.push(
                      "example seven",
                      "example eight"
                      );

                      --
                      | Grant Wagner <gwagner@agrico reunited.com>

                      * Client-side Javascript and Netscape 4 DOM Reference available
                      at:
                      *


                      * Internet Explorer DOM Reference available at:
                      *
                      Gain technical skills through documentation and training, earn certifications and connect with the community


                      * Netscape 6/7 DOM Reference available at:
                      * http://www.mozilla.org/docs/dom/domref/
                      * Tips for upgrading JavaScript for Netscape 7 / Mozilla
                      * http://www.mozilla.org/docs/web-deve...upgrade_2.html


                      Comment

                      • Grant Wagner

                        #12
                        Re: How can I read array values without using key/index value?

                        Grant Wagner wrote:
                        [color=blue]
                        > Thomas 'PointedEars' Lahn wrote:
                        >[color=green]
                        > > Lee wrote:
                        > >[color=darkred]
                        > > > example[0]="example one";
                        > > > example[1]="example two";
                        > > > example[2]="example three";
                        > > > example[3]="example four";
                        > > > example[4]="example five";
                        > > > example[5]="example six";
                        > > >
                        > > > or:
                        > > >
                        > > > example[example.length]="example one";
                        > > > example[example.length]="example two";
                        > > > example[example.length]="example three";
                        > > > example[example.length]="example four";
                        > > > example[example.length]="example five";
                        > > > example[example.length]="example six";[/color]
                        > >
                        > > Oh my. Why not
                        > >
                        > > var example =
                        > > ["example one",
                        > > "example two",
                        > > "example three",
                        > > "example four",
                        > > "example five",
                        > > "example six"];
                        > >
                        > > ?
                        > >
                        > > PointedEars[/color]
                        >
                        > Or, if you add elements in several "blocks" and can't always
                        > create a new Array:
                        >
                        > var a = new Array(); // [];
                        > a.push(
                        > "example one",
                        > "example two",
                        > "example three",
                        > "example four",
                        > "example five",
                        > "example six"
                        > );
                        > // more code
                        > a.push(
                        > "example seven",
                        > "example eight"
                        > );[/color]

                        Forgot to mention, the Array object doesn't support the push() method in IE on the Mac,
                        and while it's mostly a dead browser, the problem can be worked around with:

                        var test = new Array();
                        if (typeof test.push == "undefined" ) {
                        function push() {
                        for (var i = 0 ; i < arguments.lengt h ; i++) {
                        this[this.length] = arguments[i];
                        }
                        }
                        Array.prototype .push = push;
                        }

                        I'm sure this could be streamlined, but I don't have a Mac to test it on, and I'm sure
                        the above works because we've verified it.

                        if (typeof (new Array()).push == "undefined" ) {
                        Array.prototype .push = function() {
                        for (var i = 0 ; i < arguments.lengt h ; i++) {
                        this[this.length] = arguments[i];
                        }
                        }
                        }

                        would probably work as well, although, as I said, I don't have a way to test
                        Array.prototype .push = function()...

                        --
                        | Grant Wagner <gwagner@agrico reunited.com>

                        * Client-side Javascript and Netscape 4 DOM Reference available at:
                        * http://devedge.netscape.com/library/...ce/frames.html
                        * Internet Explorer DOM Reference available at:
                        * http://msdn.microsoft.com/workshop/a...ence_entry.asp
                        * Netscape 6/7 DOM Reference available at:
                        * http://www.mozilla.org/docs/dom/domref/
                        * Tips for upgrading JavaScript for Netscape 7 / Mozilla
                        * http://www.mozilla.org/docs/web-deve...upgrade_2.html


                        Comment

                        • Randell D.

                          #13
                          Re: How can I read array values without using key/index value?


                          "Douglas Crockford" <nospam@covad.n et> wrote in message
                          news:835a7$3fda 5edd$44a4afc5$6 684@msgid.megan ewsservers.com. ..[color=blue][color=green][color=darkred]
                          > > > Buy a book, man. You are dealing with several misunderstandin gs.
                          > > > Some people enjoy being willfully ignorant.
                          > > > I sense that you are not one of those people.[/color][/color]
                          >[color=green]
                          > > I'm broke - no work and little savings to carry me forward until a work
                          > > permit gets approved... Secondly, I have put a javascript book on my[/color][/color]
                          xmas[color=blue][color=green]
                          > > wish list... I'm reliant on old javascript books and online guides that[/color][/color]
                          I[color=blue][color=green]
                          > > read. I guess my nutty idea of using letters as subscripts might have
                          > > originated from PHP but, while I can't recall where, I thought I'd seen[/color][/color]
                          an[color=blue][color=green]
                          > > examlpe of someone who had done such (ie used letters (and not[/color][/color]
                          variables) to[color=blue][color=green]
                          > > identify elements in an array.[/color]
                          >
                          > You are breaking my heart. That is about the saddest story I have ever[/color]
                          seen in[color=blue]
                          > this newsgroup.
                          >[/color]

                          Smart ass!


                          Comment

                          • Randell D.

                            #14
                            Re: How can I read array values without using key/index value?


                            "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote in message
                            news:3FDB55DF.3 060105@PointedE ars.de...[color=blue]
                            > Lee wrote:
                            >[color=green]
                            > > example[0]="example one";
                            > > example[1]="example two";
                            > > example[2]="example three";
                            > > example[3]="example four";
                            > > example[4]="example five";
                            > > example[5]="example six";
                            > >
                            > > or:
                            > >
                            > > example[example.length]="example one";
                            > > example[example.length]="example two";
                            > > example[example.length]="example three";
                            > > example[example.length]="example four";
                            > > example[example.length]="example five";
                            > > example[example.length]="example six";[/color]
                            >
                            > Oh my. Why not
                            >
                            > var example =
                            > ["example one",
                            > "example two",
                            > "example three",
                            > "example four",
                            > "example five",
                            > "example six"];
                            >
                            > ?
                            >
                            >
                            > PointedEars[/color]

                            Thanks... but I think I prefer the
                            [color=blue][color=green]
                            > > example[example.length]="example one";
                            > > example[example.length]="example two";
                            > > example[example.length]="example three";
                            > > example[example.length]="example four";
                            > > example[example.length]="example five";
                            > > example[example.length]="example six";[/color][/color]

                            It allows me to include more than one javascript file that contains
                            additional elements for the array which are appended without having to
                            re-jig the numeric indexes...



                            Comment

                            • Thomas 'PointedEars' Lahn

                              #15
                              Re: How can I read array values without using key/index value?

                              Randell D. wrote:
                              [color=blue]
                              > "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote in message
                              > news:3FDB55DF.3 060105@PointedE ars.de...[/color]

                              It is called attribution _line_.
                              [color=blue][color=green]
                              >> Oh my. Why not
                              >>
                              >> var example =
                              >> ["example one",
                              >> "example two",
                              >> "example three",
                              >> "example four",
                              >> "example five",
                              >> "example six"];
                              >>
                              >> ?
                              >> [...][/color]
                              >
                              > Thanks... but I think I prefer the
                              >[color=green][color=darkred]
                              >> > example[example.length]="example one";
                              >> > [...][/color][/color]
                              >
                              > It allows me to include more than one javascript file that contains
                              > additional elements for the array which are appended without having to
                              > re-jig the numeric indexes...[/color]

                              You can use both without using indexes on assignment, provided that the
                              first comes first.


                              PointedEars

                              P.S.
                              Please trim your quotes: <http://www.netmeister. org/news/learn2quote.htm l>

                              And use an e-mail address, not spoiling existing namespaces
                              (here: share.com) and not pissing off communicative people:
                              <http://www.interhack.n et/pubs/munging-harmful/>

                              Comment

                              Working...