johnsmith153 Posted January 8, 2009 Share Posted January 8, 2009 I think I know what I am on about: I want to return values from a database and set arrays named as this: db returns "James Smith", "Dave Brown" set arrays: $James_Smith=array(); $Dave_Brown=array(); (values returned will definitely be unique) Quote Link to comment https://forums.phpfreaks.com/topic/139999-dynamic-naming-of-array/ Share on other sites More sharing options...
johnsmith153 Posted January 8, 2009 Author Share Posted January 8, 2009 $a="Jim"; $$a="value"; Quote Link to comment https://forums.phpfreaks.com/topic/139999-dynamic-naming-of-array/#findComment-732431 Share on other sites More sharing options...
PFMaBiSmAd Posted January 8, 2009 Share Posted January 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/139999-dynamic-naming-of-array/#findComment-732438 Share on other sites More sharing options...
johnsmith153 Posted January 8, 2009 Author Share Posted January 8, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/139999-dynamic-naming-of-array/#findComment-732449 Share on other sites More sharing options...
GingerRobot Posted January 8, 2009 Share Posted January 8, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/139999-dynamic-naming-of-array/#findComment-732456 Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 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 =) Quote Link to comment https://forums.phpfreaks.com/topic/139999-dynamic-naming-of-array/#findComment-732461 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.