Defining array index constants or using associatve arrays

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

    Defining array index constants or using associatve arrays

    I honestly wasn't able to find an answer for this design question using
    Google and Google Groups, so I apologize if it is asked too frequently :)

    Anyway: Let's say I have a multidimensiona l array of the following kind:

    $people = array(); // maps age and e-mail address to names
    $people["Paul"] = array(21, "paul@foo.bar") ;
    $people["Linda"] = array(22, "linda@bar.foo" );
    $people["Max"] = array(19, "max@foobar.foo bar");

    That obviously isn't satisfying. I don't want to remember the order in
    which the data is saved for each array item. That would be tedious and
    error-prone, I believe:

    echo $people["Max"][1]; // a magic number...

    So I might want to use an associatve array for each one:

    $people = array();
    $people["Paul"] = array("age" => 21, "mail" => "paul@foo.bar") ;
    $people["Linda"] = array("age" => 22, "mail" => "linda@bar.foo" );
    $people["Max"] = array("age" => 19, "mail" => "max@foobar.foo bar");

    echo $people["Max"]["mail"];

    Much more legible. As someone with some C++ and C background, that still
    doesn't strike me as perfect, though. What if there are hundreds of
    people to save? Isn't it a big waste (plus a performance hit) to put all
    those identical strings ("age" and "mail") into the array?

    I may as well define constants for the array indices:

    define("PEOPLE_ INDEX_AGE", 0);
    define("PEOPLE_ INDEX_MAIL", 1);

    $people = array(); // maps age and e-mail address to names
    $people["Paul"] = array(21, "paul@foo.bar") ;
    $people["Linda"] = array(22, "linda@bar.foo" );
    $people["Max"] = array(19, "max@foobar.foo bar");

    echo $people["Max"][PEOPLE_INDEX_MA IL];


    Is there any reason not to use this solution? My C++ and C thinking
    might affect my reasoning in a way that is not good for PHP programming.
    Any help on this design issue would be most appreciated, as would be
    links where I can learn more about such basic PHP design guidelines.


    --
    Christian Hackl
  • Jean-Baptiste Nizet

    #2
    Re: Defining array index constants or using associatve arrays

    I would combine your second and your third solutions:

    $people["Paul"] = array(PEOPLE_IN DEX_AGE => 21, PEOPLE_INDEX_MA IL =>
    "p...@foo.bar") ;
    $people["Linda"] = array(PEOPLE_IN DEX_AGE => 22, PEOPLE_INDEX_MA IL =>
    "l...@bar.foo") ;
    $people["Max"] = array(PEOPLE_IN DEX_AGE => 19, PEOPLE_INDEX_MA IL =>
    "m...@foobar.fo obar");

    Or, even better IMHO, I would use classes:

    class Person {
    var $age;
    var $address;

    function Person($age, $address) {
    $this->age = $age;
    $this->address = $address;
    }

    function getAge() {
    return $this->age
    }

    function getAddress() {
    return $this->address;
    }
    }

    $people["Paul"] = new Person(21, "p...@foo.bar") ;

    Comment

    • Chung Leong

      #3
      Re: Defining array index constants or using associatve arrays

      "Christian Hackl" <hacki@sbox.tug raz.at> wrote in message
      news:42136241$0 $22324$91cee783 @newsreader01.h ighway.telekom. at...[color=blue]
      > So I might want to use an associatve array for each one:
      >
      > $people = array();
      > $people["Paul"] = array("age" => 21, "mail" => "paul@foo.bar") ;
      > $people["Linda"] = array("age" => 22, "mail" => "linda@bar.foo" );
      > $people["Max"] = array("age" => 19, "mail" => "max@foobar.foo bar");
      >
      > echo $people["Max"]["mail"];
      >
      > Much more legible. As someone with some C++ and C background, that still
      > doesn't strike me as perfect, though. What if there are hundreds of
      > people to save? Isn't it a big waste (plus a performance hit) to put all
      > those identical strings ("age" and "mail") into the array?
      >
      > I may as well define constants for the array indices:
      >
      > define("PEOPLE_ INDEX_AGE", 0);
      > define("PEOPLE_ INDEX_MAIL", 1);
      >
      > $people = array(); // maps age and e-mail address to names
      > $people["Paul"] = array(21, "paul@foo.bar") ;
      > $people["Linda"] = array(22, "linda@bar.foo" );
      > $people["Max"] = array(19, "max@foobar.foo bar");
      >
      > echo $people["Max"][PEOPLE_INDEX_MA IL];
      >
      >
      > Is there any reason not to use this solution? My C++ and C thinking
      > might affect my reasoning in a way that is not good for PHP programming.
      > Any help on this design issue would be most appreciated, as would be
      > links where I can learn more about such basic PHP design guidelines.[/color]

      No reason not to. You just don't gain much by going this route. PHP arrays
      are very different from C arrays. They are hash tables. Even when you access
      an element by a numeric index, PHP would still perform a hash look up. The
      only thing you save speedwise is in the generation of the hash key. And if
      you use a constant, you cough the saving back up, since the value of PHP
      constants are obtained from a hash table at runtime. You do save on memory
      though, since the key isn't stored multiple times in the hash table.

      Objects are also implement using hash tables. There isn't a great difference
      between $person->age and $person['age']. I prefer using objects for storing
      data, since it makes more sense conceptually. Object properties are also
      easier to interpolate into strings ("$person->age years old" vs
      "{$person['age']} years old").


      Comment

      • Christian Hackl

        #4
        Re: Defining array index constants or using associatve arrays

        Chung Leong wrote:
        [color=blue]
        > No reason not to. You just don't gain much by going this route. PHP
        > arrays are very different from C arrays. They are hash tables. Even
        > when you access an element by a numeric index, PHP would still
        > perform a hash look up. The only thing you save speedwise is in
        > the generation of the hash key. And if you use a constant, you cough
        > the saving back up, since the value of PHP constants are obtained
        > from a hash table at runtime. You do save on memory though, since
        > the key isn't stored multiple times in the hash table.[/color]

        Thanks for yours and Jean-Baptiste's answers. That's most interesting.
        I'll probably go the arr["Index"] route then, since I am not to keen on
        mixing classes into my currently purely procedural PHP code.

        P.S.: Sorry for posting through Google, but my news server ate this
        whole thread for reasons unknown (hence the late reply) :(

        --
        Christian Hackl

        Comment

        • Matt Mitchell

          #5
          Re: Defining array index constants or using associatve arrays


          "Christian Hackl" <hacki@sbox.tug raz.at> wrote in message
          news:1108775153 .386468.299660@ g14g2000cwa.goo glegroups.com.. .[color=blue]
          > Thanks for yours and Jean-Baptiste's answers. That's most interesting.
          > I'll probably go the arr["Index"] route then, since I am not to keen on
          > mixing classes into my currently purely procedural PHP code.[/color]

          Better to go $array['index'] as single quotes prevent evaluating the string
          first for interpolations like $array["index"] would.

          Unless you want to do that of course!

          Matt


          Comment

          Working...