Jump to content

Variable to array without naming it ??


mwl707

Recommended Posts

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

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.

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

 

 

Archived

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

×
×
  • 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.