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
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
Comment