referencing an array element with a string variable

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

    referencing an array element with a string variable

    I've been researching this for hours and can't seem to find the right
    syntax.

    I need to retrieve a value of an array by referencing the element using a
    string variable.

    For example:

    $data['lastname'] = 'hank';
    $element = 'lastname';
    echo $data[$element];

    Outputs:

    hank

    Seems straightforward enough, but the solution escapes me.

    Someone clue me in--thanks!




  • Shelly

    #2
    Re: referencing an array element with a string variable


    "Bosconian" <bosconian@plan etx.com> wrote in message
    news:97GdnQ-W6r5A7kHfRVn-tA@comcast.com. ..[color=blue]
    > I've been researching this for hours and can't seem to find the right
    > syntax.
    >
    > I need to retrieve a value of an array by referencing the element using a
    > string variable.
    >
    > For example:
    >
    > $data['lastname'] = 'hank';
    > $element = 'lastname';
    > echo $data[$element];
    >
    > Outputs:
    >
    > hank
    >
    > Seems straightforward enough, but the solution escapes me.
    >
    > Someone clue me in--thanks![/color]

    echo $data["$element"];

    Shelly


    Comment

    • ZeldorBlat

      #3
      Re: referencing an array element with a string variable

      Both Shelly's and yours output the same answer -- which seems to be the
      answer you're looking for.

      What is the problem?

      Comment

      • Ken Robinson

        #4
        Re: referencing an array element with a string variable



        Bosconian wrote:[color=blue]
        > I've been researching this for hours and can't seem to find the right
        > syntax.
        >
        > I need to retrieve a value of an array by referencing the element using a
        > string variable.
        >
        > For example:
        >
        > $data['lastname'] = 'hank';
        > $element = 'lastname';
        > echo $data[$element];
        >
        > Outputs:
        >
        > hank
        >
        > Seems straightforward enough, but the solution escapes me.[/color]

        That should work fine. What problems are you encountering? What are you
        trying to do?

        Ken

        Comment

        • Bosconian

          #5
          Re: referencing an array element with a string variable

          "Ken Robinson" <kenrbnsn@rbnsn .com> wrote in message
          news:1121746343 .422353.202300@ g47g2000cwa.goo glegroups.com.. .[color=blue]
          >
          >
          > Bosconian wrote:[color=green]
          > > I've been researching this for hours and can't seem to find the right
          > > syntax.
          > >
          > > I need to retrieve a value of an array by referencing the element using[/color][/color]
          a[color=blue][color=green]
          > > string variable.
          > >
          > > For example:
          > >
          > > $data['lastname'] = 'hank';
          > > $element = 'lastname';
          > > echo $data[$element];
          > >
          > > Outputs:
          > >
          > > hank
          > >
          > > Seems straightforward enough, but the solution escapes me.[/color]
          >
          > That should work fine. What problems are you encountering? What are you
          > trying to do?
          >
          > Ken
          >[/color]

          Thanks to all who responded.

          Turns out the syntax was correct (as noted.) My array was in fact not being
          defined correctly so the element was therefore empty.

          Sorry for wasting your time.


          Comment

          Working...