Jump to content

Returning a variable value from an associative array


team-banzai

Recommended Posts

OK, So we all agree that the use of variables is to avoid hard coded instances as much as possible.So in the following example: <?php $cars = array ( "Toyota"=>array ( "Prius", "Camery" ), "Honda"=>array ( "Accord" ), "Ford"=>array ( "Focus", "Apire" ) ); echo "Is " . $cars['Toyota'][1] . " a Toyota car?"; ?>I would like to replace the hard coded use of "Toyota" in the bottom line(the echo statement) with a variableI tried the following: echo "Is " . $cars['Toyota'][1] . " a " . $cars[0] . " car?"; // this failed to return any value in that spaceI then tried the following: echo "Is " . $cars['Toyota'][1] . " a " . $cars[Toyota] . " car?"; // this also failed to return a value in that spaceI then tried the following: echo "Is " . $cars['Toyota'][1] . " a " . $cars['Toyota'] . " car?"; // this also failed to return a value in that spaceHow do I get the array "cars" to return the value "Toyota" such that the output reads: "Is Camery a Toyota car?"And also responds to some form of "echo "Is " . $cars['Ford'][0] . " a " . $cars[0] . " car?";" as: "Is Focus a Ford car?"Thank you for your help!Marc

Link to comment
Share on other sites

But, you're writing Toyota literally when referencing the array earlier in the statement... wouldn't it be more logical to do something like:

$car = "Toyota";echo "Is " . $cars[$car][1] . " a " . $car . " car?";

It is true that you should try to avoid literals everywhere, but your code still needs to make sense. Think of what your code is meant to do, and there will be a logical way of getting the value for the name of the car. For example, if you just wanted to loop through all the models:

foreach ($cars as $brand => $models) {	foreach ($models as $model) echo "Is " . $model . " a " . $brand . " car?";}

Link to comment
Share on other sites

But, you're writing Toyota literally when referencing the array earlier in the statement... wouldn't it be more logical to do something like:
$car = "Toyota";echo "Is " . $cars[$car][1] . " a " . $car . " car?";

Yes, it is a literal in the first example...which is an example of what I am trying to get away from(as I said) The Associative Array has 'Toyota' as a value already. Creating another variable is nothing adding more duplication to use a value that already exists. It is a waste of memory, computing cycles and effort. Despite being a key value, the Toyota key value is a variable. So it "should be" able to be used as such... No?
It is true that you should try to avoid literals everywhere, but your code still needs to make sense. Think of what your code is meant to do, and there will be a logical way of getting the value for the name of the car. For example, if you just wanted to loop through all the models:
foreach ($cars as $brand => $models) {	foreach ($models as $model) echo "Is " . $model . " a " . $brand . " car?";}

But I do not want to cycle through all the models...as, again, I said in my post.So foreach does not help me, and the idea of spending the extra memory and cycles in setting up an extra variable structure.Marc
Link to comment
Share on other sites

Array keys are strings that can be taken from variables too. So, what you can do is define the literal in another variable, and use that:

$targetBrand = 'Toyota';$cars = array($targetBrand =>array("Prius","Camery"),"Honda"=>array("Accord"),"Ford"=>array("Focus","Apire"));foreach ($cars as $brand => $models) {	foreach ($models as $model) echo "Is " . $model . " a " . $targetBrand . " car?";}

Now, that particular example assumes you know in advance your target brand (in this case, Toyota) while creating the array, but that's probably not going to be the case in a real application - in a real application, you'll have an array (or something else) which you must check with a separately supplied value. If you knew in advance what you were checking, you wouldn't need to do the check on the first place.On a slightly more twisted example, you can get the keys of an array with the key() function (if you want the "current" key... long story) or array_keys() (if you want to have all keys as the values of a new array, from which you can get whichever you want). So, for example, you could do:

$targetBrand = key($cars);foreach ($cars as $brand => $models) {	foreach ($models as $model) echo "Is " . $model . " a " . $targetBrand . " car?";}

or

$brands = array_keys($cars);foreach ($cars as $brand => $models) {	foreach ($models as $model) echo "Is " . $model . " a " . $brands[0] . " car?";}

There is something else you should keep in mind when thinking about performance - a function call is often (if not always) more CPU expensive than the creation of a new string literal, so while the above examples do what you want, they are probably more CPU expensive than you writing out the literal value as in your first example.Also, a literal or not, the contents of echo still goes into the server's RAM, as content to be given to the client. A string literal on echo will be discarded from memory (in PHP context) as soon as its there.There may also be some other things I'm missing, but the point is performance often depends on the scenario, and what's more important for you - less RAM consumption, or less CPU consumption... you can't have both. It's a balancing act. The point where people say they make performance optimizations is when they sacrifice some CPU/RAM load in order to save what would otherwise be a noticeable RAM/CPU load.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...