问题
Here's what I'd like to do
for($i = 1; $i < 4; $i++) {
$featuredImage$i = $featured['featuredImage$i'];
$featuredText$i = $featured['featuredText$i'];
$featuredURL$i = $featured['featuredURL$i'];
}
How can do I something like that?
回答1:
Only thing you need is to learn PHP language.
This page contains everything you need: http://php.net/types.string
It worth reading regardless this particular question, as string syntax is one of most basic PHP syntax concepts and you have to learn it by heart anyway.
The only note I have to add: strictly speaking, you're gonna use a counter not as a part of variable but as a part of string, which, in it's turn, gonna be a key for the array variable. But syntactically you're composing a string out of a string and a variable.
EDIT
Oh. I didn't notice leftmost variable.
Only when I started writing another suggestion, I was able to see it.
Well, you may also consider using nested array.
It could be used this way
foreach ($featured as $item) {
echo $item['featuredImage'];
echo $item['featuredText'];
echo $item['featuredURL'];
}
or do whatever you want to do with these variables.
It would be nice to see the full context which lead you to this question, thus it will be possible to give you best solution for your particular case.
May be you will need dramatically redesign your database...
回答2:
Something like
for($i = 1; $i < 4; $i++) {
${featuredImage . $i} = $featured['featuredImage' . $i];
${featuredText. $i} = $featured['featuredText' . $i];
${featuredURL . $i} = $featured['featuredURL' . $i];
}
回答3:
You need to concatenate the variable onto the end of the string, as so:
for($i = 1; $i < 4; $i++) {
$featuredImage$i = $featured['featuredImage'.$i];
$featuredText$i = $featured['featuredText'.$i];
$featuredURL$i = $featured['featuredURL'.$i];
}
Remember, variables cannot be used inside strings with single quotes, if you wanted to put a variable within a string use double quotes instead, for example:
$world="World!";
$str1='Hello $world'; // outputs "Hello $world"
$str2="Hello $world"; // outputs "Hello World!"
You can use variables inside strings with doubles quotes, sometimes this is easier than breaking in and out of the string, however with single quotes you must concatenate it in place;
回答4:
These will work:
for($i = 1; $i < 4; $i++) {
$featuredImage$i = $featured["featuredImage$i"]; // $variable works inside double quotes
$featuredText$i = $featured['featuredText' . $i]; // single quote and string concatenation
$featuredURL$i = $featured["featuredURL{$i}"]; // $variable can be enclosed inside {} for cases such as $featured["featuredURL{$i}_href"]
// to have php substitute the variable $i instead of $i_href
}
回答5:
i got Answer : Try it
${"featuredImage" . $i} = $featured['featuredImage' . $i];
${"featuredText". $i} = $featured['featuredText' . $i];
${"featuredURL" . $i} = $featured['featuredURL' . $i];
回答6:
for($i = 1; $i < 4; $i++) {
$featuredImage$i = $featured['featuredImage' .$i];
$featuredText$i = $featured['featuredText' .$i];
$featuredURL$i = $featured['featuredURL' .$i];
}
来源:https://stackoverflow.com/questions/4990901/use-counter-as-part-of-variable