/    Sign up×
Community /Pin to ProfileBookmark

variable question

in a for each loop, i have an array going through with $key=>$value.

for every $key values of the array, there is a variable with the same exact name that i’d like to be populated with the $value.

foreach($Station_Info as $key=>$value)
$this->{$key} = $value;

where {$key} is, i’d like for some how it reference the variable. again, each $key value is the same as every variable name in that class.

edit: what’s useful about what i’m doing is that i won’t have to do this:

$this->StationID = $Station_info[‘StationID ‘];
$this->Name = $Station_info[‘Name’];
$this->Address = $Station_info[‘Address’];
$this->City = $Station_info[‘City’];
… continue for 15 more lines

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@GavinPearceMay 28.2008 — Im confused a little because the way I read it you just answered your own question. This what you're looking for?

[code=php]
foreach($Station_Info as $key=>$value) {
$this->$key = $value;
}
[/code]
Copy linkTweet thisAlerts:
@ixxalnxxiauthorMay 28.2008 — oh... i did not know it was possible to use multiple $ in a class-member variable.. should have tested before i said anything.

thanks for the response! =)
Copy linkTweet thisAlerts:
@GavinPearceMay 28.2008 — Didnt know if it would work myself, so if it does, im as happy as you are. :-D
×

Success!

Help @ixxalnxxi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...