PHP foreach multiple each and as
I'm trying to save code and have multiple php for each going on at the same time. Is it possible.
$holes9 = string(lots of information separated by,)
$stroke = string(other info separated by,)
$index = string(other info separated by,)
$holes9 = explode(",",$holes9);
foreach ($holes9 as $holes) {
开发者_如何学Python echo '<div class="hole"><b>'.$holes.'</b></div>
<div class="stroke"></div>
<div class="index"></div>
';
};
As you can see my foreach only applies to $holes9 as $holes. How do I get the other two bits of info.
If $holes
, $stroke
and $index
have the same amount of "elements", it's enough with single foreach
:
$holes9 = string(lots of information separated by,)
$stroke = string(other info separated by,)
$index = string(other info separated by,)
$holes9 = explode(",",$holes9);
$stroke = explode(",",$stroke);
$index = explode(",",$index);
foreach ($holes9 as $id => $holes) {
echo '
<div class="hole"><b>'.$holes9[$id].'</b></div>
<div class="stroke">'.$stroke[$id].'</div>
<div class="index">'.$index[$id].'</div>
';
};
If the strings are of the same separation length (i.e. they will have the same number of commas ,
), then you can do it like this:
$holes9 = 'lots, of information, separated, by a, comma';
$stroke = 'other, info, separated, by, a comma';
$index = 'another, info, string, separated by, a comma';
$holes9 = explode(',',$holes9);
$strokes = explode(',', $strokes);
$index = explode(',', $index);
foreach ($holes9 as $id => $holes) {
echo '<div class="hole"><b>'.$holes.'</b></div>'.
'<div class="stroke">'.$strokes[$id].'</div>'.
'<div class="index">'.$index[$id].'</div>';
};
$holes9 = explode(",",$holes9);
$stroke = explode(",",$stroke); // explode these as well
$index = explode(",",$index);
foreach ($holes9 as $n => $holes) // take advantage of referencing the numeric key
{
echo '<div class="hole"><b>'.$holes.'</b></div>';
echo '<div class="stroke">'.$stroke[$n].'</div>';
echo '<div class="index">'.$index[$n].'</div>';
}
Since explode()
will generate numeric keys, make use of the foreach's ability to grab a key and a value. The first echo uses the value, then re-use the key (in this case $n
) to reference the same element indexes in the other two arrays ($stroke
and $index
, assuming these will have the same number of elements.)
If the length of all the lists is the same you can do this:
$holes9 = string(lots of information separated by,)
$stroke = string(other info separated by,)
$index = string(other info separated by,)
$holes9 = explode(",",$holes9);
foreach ($holes9 as $key => $holes) {
echo '<div class="hole"><b>'.$holes.'</b></div>
<div class="stroke">'.$stroke[$key].'</div>
<div class="index">'.$index[$key].'</div>
';
};
Assume that your 3 strings contain the same number of elements you can use for instead of foreach like that :
$holes9 = explode(",",$holes9);
$stroke = explode(",",$stroke);
$index = explode(",",$index);
for($i =0; $i < ;$i++){
echo '<div class="hole"><b>'.$holes9[$i].'</b></div>
<div class="stroke">'.$stroke[$i].'</div>
<div class="index">'.$index[$i].'</div>';
}
精彩评论