Use counter as part of variable
Here's what I'd like to do
for($i = 1; $i < 4; $i++) {
$featuredImage$i = $featured['featuredImage$i'];
$featuredText$i = $featured['featuredText$i'];
$featuredU开发者_如何转开发RL$i = $featured['featuredURL$i'];
}
How can do I something like that?
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...
Something like
for($i = 1; $i < 4; $i++) {
${featuredImage . $i} = $featured['featuredImage' . $i];
${featuredText. $i} = $featured['featuredText' . $i];
${featuredURL . $i} = $featured['featuredURL' . $i];
}
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;
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
}
i got Answer : Try it
${"featuredImage" . $i} = $featured['featuredImage' . $i];
${"featuredText". $i} = $featured['featuredText' . $i];
${"featuredURL" . $i} = $featured['featuredURL' . $i];
for($i = 1; $i < 4; $i++) {
$featuredImage$i = $featured['featuredImage' .$i];
$featuredText$i = $featured['featuredText' .$i];
$featuredURL$i = $featured['featuredURL' .$i];
}
精彩评论