Jump to content

Recommended Posts

It would be much better to create an array of arrays, where the main index name is the name you are getting from the data.

 

If you could do what you ask for arrays, you must then be able to access that data in order to use it. How are you going to know the names of the variables that were created and how are you going to prevent them from overwriting existing variables in your code that happen to have the same name as what you are dynamically creating? Accessing dynamically created variables requires complicated and slow code. Using an array (or an array of arrays) simplifies the code because you can get a list of what was created by accessing the keys or in most cases you can use a foreach() loop to iterate over all the created data without needing to know the names of data before hand.

PFMaBiSmAd,

 

I think you are telling me to:

return values from db and store in array as such: $personName["Jim"]="22"; (value is their age / key is their name)

-Then later simply echo $personName[$selectedName];

 

However I need it to sort of hold a third value:

 

Imagine if I need to store under their city/state/town:

(I will use English name)

 

$London["Jim"]="22";

$Birmingham["James"]="30";

$London["Dave"]="24";

(I would probably call the array "$locationLondon" etc but I hope this explains it)

PFMaBiSmAd,

 

I think you are telling me to:

return values from db and store in array as such: $personName["Jim"]="22"; (value is their age / key is their name)

-Then later simply echo $personName[$selectedName];

 

However I need it to sort of hold a third value:

 

Imagine if I need to store under their city/state/town:

(I will use English name)

 

$London["Jim"]="22";

$Birmingham["James"]="30";

$London["Dave"]="24";

(I would probably call the array "$locationLondon" etc but I hope this explains it)

 

So store more values?

 

$details['Tom'] = array('location'=>'london','age'=>22);
$details['Dick'] = array('location'=>'timbuktu','age'=>25);

Multi Dimmensional array:

 

<?php
$towns = array("london" => array("jim" => array("city" => "testCity", "state" => "testState", "dave" => array("city" => "testCityda", "state" => "testStateda")), "Birmingham" => array("james" => array("city" => "testCityJa", "state" => "testStateJa")));

foreach ($towns as $town => $occupants) {
    echo "Town: " . $town . "<br />";
    if (is_array($occupants)) {
         foreach ($occupants as $occupant => $info) {
            echo "Occupant " . $occupant . "<br />";
             if (is_array($info)) {
                  echo "    City" . $info['city'] . "<br />";
                  echo "    State" . $info['state'] . "<br /><br />";
             }
         }
    }
}

?>

 

Should display them all. Now this is just an example for you to see/understand how it works. When you would get it from the DB, you could create a new array $towns where the index is $returnDBValue['town'] so $towns[$returnDBValue['town']][$returnDBValue['name']] = $returnDBValue  which would make it a multi dimmensional array which is indexed first by town then by name.

 

Now you may not have to go that far to do what you want, but that is the basic gist.

 

The simpler/easier approach is GingerRobot's example, that is how you should do it. My example was just to help you understand the power of arrays =)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.