mwl707 Posted October 15, 2009 Share Posted October 15, 2009 Hi I have a variable that I would like to make an array without naming the array directly . I hope this makes sense ? $count = 0 ; $text = "people" . "[$count]" ; $$text = "John" ; hopefully I want to be able to do this now echo $people[0] ; and the result should be 'John' ?? But I get nothing at all Can anyone tell me if I can do this ?? Thanks Link to comment https://forums.phpfreaks.com/topic/177771-variable-to-array-without-naming-it/ Share on other sites More sharing options...
dymon Posted October 15, 2009 Share Posted October 15, 2009 This can be a way of accessing it, if that helps: print ${"people[0]"}; Link to comment https://forums.phpfreaks.com/topic/177771-variable-to-array-without-naming-it/#findComment-937380 Share on other sites More sharing options...
mwl707 Posted October 15, 2009 Author Share Posted October 15, 2009 The problem is that $people[0] has no value at all Link to comment https://forums.phpfreaks.com/topic/177771-variable-to-array-without-naming-it/#findComment-937383 Share on other sites More sharing options...
cags Posted October 15, 2009 Share Posted October 15, 2009 Why do you wish to create it without declaring it, if you then wish to access it using $people[0]? As far as I can see, the only reason to dynamically create a variable is if you aren't implicity calling it. Link to comment https://forums.phpfreaks.com/topic/177771-variable-to-array-without-naming-it/#findComment-937388 Share on other sites More sharing options...
mwl707 Posted October 15, 2009 Author Share Posted October 15, 2009 I am getting a list from a table in a database , Each of the items in the list need to become arrays. As the list will change I cannot declare the arrays first Mick Link to comment https://forums.phpfreaks.com/topic/177771-variable-to-array-without-naming-it/#findComment-937389 Share on other sites More sharing options...
mwl707 Posted October 15, 2009 Author Share Posted October 15, 2009 Thanks for the PM, This code will be a part of a function that I will call several times. Rather than writing the code many time is the reason for not defining the arrays first Link to comment https://forums.phpfreaks.com/topic/177771-variable-to-array-without-naming-it/#findComment-937393 Share on other sites More sharing options...
dymon Posted October 15, 2009 Share Posted October 15, 2009 Place a bigger code here so we could see the real needs and maybe there is an other logical way. Link to comment https://forums.phpfreaks.com/topic/177771-variable-to-array-without-naming-it/#findComment-937398 Share on other sites More sharing options...
cags Posted October 15, 2009 Share Posted October 15, 2009 My point was if you have echo $people[0]; further down your code, thats a static variable name meaning the dynamic creation of the array isn't required. Perhaps that was just an example and that isn't the actual code you'll end up using to access the item... but I think as dymon says, a more complete outlook at your objective would help. Link to comment https://forums.phpfreaks.com/topic/177771-variable-to-array-without-naming-it/#findComment-937411 Share on other sites More sharing options...
dgoosens Posted October 15, 2009 Share Posted October 15, 2009 don't really understand either but... Couldn't you just add an if statement if(is_array($people)) { echo $people[0]; } else { echo $people; } it would be better though if you could make sure upfront that the data is either an array() or a regular variable... Link to comment https://forums.phpfreaks.com/topic/177771-variable-to-array-without-naming-it/#findComment-937416 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.