Associative Array In Class

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

    Associative Array In Class

    In PHP Version 4.3.6 I am trying get a value from a class member that
    is an associative array...

    class MyClass
    {
    $var row;

    function MyClass()
    {
    $this->$row = array("A"=>"App le", "B"=>"Bob") ;

    echo "One: " . $this->$row["A"] . "<br>";
    echo "Two: " . $this->row["A"] . "<br>";

    $cache = $this->$row;
    echo "Three: " . $cache ["A"] . "<br>";
    }
    }

    But this is the output...

    One: Array
    Two:
    Three: Apple

    How can I get at the value of the associative array without making a
    local copy first!

    Corey
  • neur0maniak

    #2
    Re: Associative Array In Class

    corey wrote:[color=blue]
    > In PHP Version 4.3.6 I am trying get a value from a class member that
    > is an associative array...
    >
    > class MyClass
    > {
    > $var row;
    >
    > function MyClass()
    > {
    > $this->$row = array("A"=>"App le", "B"=>"Bob") ;
    >
    > echo "One: " . $this->$row["A"] . "<br>";
    > echo "Two: " . $this->row["A"] . "<br>";
    >
    > $cache = $this->$row;
    > echo "Three: " . $cache ["A"] . "<br>";
    > }
    > }
    >
    > But this is the output...
    >
    > One: Array
    > Two:
    > Three: Apple
    >
    > How can I get at the value of the associative array without making a
    > local copy first!
    >
    > Corey[/color]

    Don't use a $ after the -> symbol

    Try this:
    class MyClass
    {
    $var row;

    function MyClass()
    {
    $this->row = array("A"=>"App le", "B"=>"Bob") ;

    echo "One: " . $this->row["A"] . "<br>";
    echo "Two: " . $this->row["A"] . "<br>";

    $cache = $this->row;
    echo "Three: " . $cache ["A"] . "<br>";
    }
    }

    Comment

    • corey

      #3
      Re: Associative Array In Class

      I tried that in the original example... It is what is output after
      "Two:" but that ouputs nothing if you look at the output in my first
      post.

      Corey
      [color=blue]
      > corey wrote:[color=green]
      > > In PHP Version 4.3.6 I am trying get a value from a class member that
      > > is an associative array...
      > >
      > > class MyClass
      > > {
      > > $var row;
      > >
      > > function MyClass()
      > > {
      > > $this->$row = array("A"=>"App le", "B"=>"Bob") ;
      > >
      > > echo "One: " . $this->$row["A"] . "<br>";
      > > echo "Two: " . $this->row["A"] . "<br>";
      > >
      > > $cache = $this->$row;
      > > echo "Three: " . $cache ["A"] . "<br>";
      > > }
      > > }
      > >
      > > But this is the output...
      > >
      > > One: Array
      > > Two:
      > > Three: Apple
      > >
      > > How can I get at the value of the associative array without making a
      > > local copy first!
      > >
      > > Corey[/color]
      >
      > Don't use a $ after the -> symbol
      >
      > Try this:
      > class MyClass
      > {
      > $var row;
      >
      > function MyClass()
      > {
      > $this->row = array("A"=>"App le", "B"=>"Bob") ;
      >
      > echo "One: " . $this->row["A"] . "<br>";
      > echo "Two: " . $this->row["A"] . "<br>";
      >
      > $cache = $this->row;
      > echo "Three: " . $cache ["A"] . "<br>";
      > }
      > }[/color]

      Comment

      • Anders K. Madsen

        #4
        Re: Associative Array In Class

        On 18 Jul 2004 15:20:32 -0700
        coreydalejohnso n@yahoo.com (corey) wrote:
        [color=blue]
        > I tried that in the original example... It is what is output after
        > "Two:" but that ouputs nothing if you look at the output in my first
        > post.
        >[/color]

        I think he meant here:[color=blue][color=green][color=darkred]
        > > > $this->$row = array("A"=>"App le", "B"=>"Bob") ;[/color][/color][/color]

        It should be $this->row = array("A" => "Apple", "B" => "Bob");
        The other evaluates to $this->"" since $row is empty.

        --
        Anders K. Madsen --- http://lillesvin.linux.dk

        "There are 10 types of people in the world.
        Those who understand binary - and those who don't."

        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.2.4 (GNU/Linux)

        iD8DBQFA+wCdlNH Je/JASHcRAukpAJ9pl LY56vmaiPoba9SG kyfmJlCVqACfUO8 r
        H99Ds7x5jNRwi5h 8EY+VvY0=
        =zOEn
        -----END PGP SIGNATURE-----

        Comment

        • Michael Fesser

          #5
          Re: Associative Array In Class

          .oO(corey)
          [color=blue]
          >In PHP Version 4.3.6 I am trying get a value from a class member that
          >is an associative array...
          >
          >class MyClass
          >{
          > $var row;[/color]

          var $row;
          [color=blue]
          > function MyClass()
          > {
          > $this->$row = array("A"=>"App le", "B"=>"Bob") ;[/color]

          The line above creates a new unnamed (name is probably an empty string)
          property in your object ($row is not initialized => NULL) and assigns
          the array to it.

          Set error_reporting to E_ALL and you'll see a notice on that.
          [color=blue]
          > echo "One: " . $this->$row["A"] . "<br>";
          > echo "Two: " . $this->row["A"] . "<br>";[/color]

          The first of the lines above accesses this "anonymous" property and
          outputs the first element, the second accesses the real and previously
          declared property, which is still empty.

          In fact

          $this->row

          and

          $this->$row

          are completly different things. The first directly accesses a property
          named 'row', the second accesses a property, whose name is stored in the
          variable $row.

          HTH
          Micha

          Comment

          Working...