/    Sign up×
Community /Pin to ProfileBookmark

Using variable variables (I think)

Hey,

I got this problem:

while (list($key, $val) = each($_POST[“fields”])) {
$lfn_id = $key;
$content = $val;
$i++;
}

Now I would like the $lfn_id and the $content to be $lfn_id1, $content1 and in the next step $lfn_id2 and $content2 and so on… how would this be possible?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@bokehNov 16.2005 — Here is one way:[code=php]<?php

$array = array('1' => 'a', '2' => 'b', '3' => 'c');

$i = 1;
foreach($array as $key => $value){
$lfn_id = 'lfn_id'.$i;
$content = 'content'.$i;
$$lfn_id = $key;
$$content = $value;
$i++;
}

// test it
print $lfn_id1.' - '.$lfn_id2.' - '.$lfn_id3.' - '.$content1.' - '.$content2.' - '.$content3;

?>[/code]
Don't you think using the original array would be easier though. If you are making these variables dynamically how are you going to keep track of which ones exist so that you can use them.
×

Success!

Help @Glyc 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,
)...