PHP « цикл foreach if(!next): Почему определяет два конечных элемента в массиве вместо одного. Как это исправить?
цикл:/**
* GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
* (http://qbnz.com/highlighter/ and http://geshi.org/)
*/
.php.geshi_code {font-family:monospace;}
.php.geshi_code .imp {font-weight: bold; color: red;}
.php.geshi_code .kw1 {color: #b1b100;}
.php.geshi_code .kw2 {color: #000000; font-weight: bold;}
.php.geshi_code .kw3 {color: #990000;}
.php.geshi_code .co1 {color: #666666; font-style: italic;}
.php.geshi_code .co2 {color: #666666; font-style: italic;}
.php.geshi_code .co3 {color: #0000cc; font-style: italic;}
.php.geshi_code .co4 {color: #009933; font-style: italic;}
.php.geshi_code .coMULTI {color: #666666; font-style: italic;}
.php.geshi_code .es0 {color: #000099; font-weight: bold;}
.php.geshi_code .es1 {color: #000099; font-weight: bold;}
.php.geshi_code .es2 {color: #660099; font-weight: bold;}
.php.geshi_code .es3 {color: #660099; font-weight: bold;}
.php.geshi_code .es4 {color: #006699; font-weight: bold;}
.php.geshi_code .es5 {color: #006699; font-weight: bold; font-style: italic;}
.php.geshi_code .es6 {color: #009933; font-weight: bold;}
.php.geshi_code .es_h {color: #000099; font-weight: bold;}
.php.geshi_code .br0 {color: #009900;}
.php.geshi_code .sy0 {color: #339933;}
.php.geshi_code .sy1 {color: #000000; font-weight: bold;}
.php.geshi_code .st0 {color: #0000ff;}
.php.geshi_code .st_h {color: #0000ff;}
.php.geshi_code .nu0 {color: #cc66cc;}
.php.geshi_code .nu8 {color: #208080;}
.php.geshi_code .nu12 {color: #208080;}
.php.geshi_code .nu19 {color:#800080;}
.php.geshi_code .me1 {color: #004000;}
.php.geshi_code .me2 {color: #004000;}
.php.geshi_code .re0 {color: #000088;}
.php.geshi_code span.xtra { display:block; }
if ($posstags) {
foreach($posttags as $tag){
if(!next($posttags)){
echo $tag->name.".";
}else{
echo $tag->name.",";
}
}
}
массив:
/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .php.geshi_code {font-family:monospace;} .php.geshi_code .imp {font-weight: bold; color: red;} .php.geshi_code .kw1 {color: #b1b100;} .php.geshi_code .kw2 {color: #000000; font-weight: bold;} .php.geshi_code .kw3 {color: #990000;} .php.geshi_code .co1 {color: #666666; font-style: italic;} .php.geshi_code .co2 {color: #666666; font-style: italic;} .php.geshi_code .co3 {color: #0000cc; font-style: italic;} .php.geshi_code .co4 {color: #009933; font-style: italic;} .php.geshi_code .coMULTI {color: #666666; font-style: italic;} .php.geshi_code .es0 {color: #000099; font-weight: bold;} .php.geshi_code .es1 {color: #000099; font-weight: bold;} .php.geshi_code .es2 {color: #660099; font-weight: bold;} .php.geshi_code .es3 {color: #660099; font-weight: bold;} .php.geshi_code .es4 {color: #006699; font-weight: bold;} .php.geshi_code .es5 {color: #006699; font-weight: bold; font-style: italic;} .php.geshi_code .es6 {color: #009933; font-weight: bold;} .php.geshi_code .es_h {color: #000099; font-weight: bold;} .php.geshi_code .br0 {color: #009900;} .php.geshi_code .sy0 {color: #339933;} .php.geshi_code .sy1 {color: #000000; font-weight: bold;} .php.geshi_code .st0 {color: #0000ff;} .php.geshi_code .st_h {color: #0000ff;} .php.geshi_code .nu0 {color: #cc66cc;} .php.geshi_code .nu8 {color: #208080;} .php.geshi_code .nu12 {color: #208080;} .php.geshi_code .nu19 {color:#800080;} .php.geshi_code .me1 {color: #004000;} .php.geshi_code .me2 {color: #004000;} .php.geshi_code .re0 {color: #000088;} .php.geshi_code span.xtra { display:block; }
Array
(
[6] => stdClass Object
(
[term_id] => 6
[name] => кот
[slug] => %d0%ba%d0%be%d1%82
[term_group] => 0
[term_taxonomy_id] => 6
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[7] => stdClass Object
(
[term_id] => 7
[name] => котопес
[slug] => %d0%ba%d0%be%d1%82%d0%be%d0%bf%d0%b5%d1%81
[term_group] => 0
[term_taxonomy_id] => 7
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[9] => stdClass Object
(
[term_id] => 9
[name] => лига
[slug] => %d0%bb%d0%b8%d0%b3%d0%b0
[term_group] => 0
[term_taxonomy_id] => 9
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[4] => stdClass Object
(
[term_id] => 4
[name] => мясо
[slug] => %d0%bc%d1%8f%d1%81%d0%be
[term_group] => 0
[term_taxonomy_id] => 4
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[3] => stdClass Object
(
[term_id] => 3
[name] => рыба
[slug] => %d1%80%d1%8b%d0%b1%d0%b0
[term_group] => 0
[term_taxonomy_id] => 3
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[11] => stdClass Object
(
[term_id] => 11
[name] => сайты
[slug] => %d1%81%d0%b0%d0%b9%d1%82%d1%8b
[term_group] => 0
[term_taxonomy_id] => 11
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[8] => stdClass Object
(
[term_id] => 8
[name] => супермен
[slug] => %d1%81%d1%83%d0%bf%d0%b5%d1%80%d0%bc%d0%b5%d0%bd
[term_group] => 0
[term_taxonomy_id] => 8
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[10] => stdClass Object
(
[term_id] => 10
[name] => чемпион
[slug] => %d1%87%d0%b5%d0%bc%d0%bf%d0%b8%d0%be%d0%bd
[term_group] => 0
[term_taxonomy_id] => 10
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[5] => stdClass Object
(
[term_id] => 5
[name] => шашлык
[slug] => %d1%88%d0%b0%d1%88%d0%bb%d1%8b%d0%ba
[term_group] => 0
[term_taxonomy_id] => 5
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
)
результат: /**
* GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
* (http://qbnz.com/highlighter/ and http://geshi.org/)
*/
.php.geshi_code {font-family:monospace;}
.php.geshi_code .imp {font-weight: bold; color: red;}
.php.geshi_code .kw1 {color: #b1b100;}
.php.geshi_code .kw2 {color: #000000; font-weight: bold;}
.php.geshi_code .kw3 {color: #990000;}
.php.geshi_code .co1 {color: #666666; font-style: italic;}
.php.geshi_code .co2 {color: #666666; font-style: italic;}
.php.geshi_code .co3 {color: #0000cc; font-style: italic;}
.php.geshi_code .co4 {color: #009933; font-style: italic;}
.php.geshi_code .coMULTI {color: #666666; font-style: italic;}
.php.geshi_code .es0 {color: #000099; font-weight: bold;}
.php.geshi_code .es1 {color: #000099; font-weight: bold;}
.php.geshi_code .es2 {color: #660099; font-weight: bold;}
.php.geshi_code .es3 {color: #660099; font-weight: bold;}
.php.geshi_code .es4 {color: #006699; font-weight: bold;}
.php.geshi_code .es5 {color: #006699; font-weight: bold; font-style: italic;}
.php.geshi_code .es6 {color: #009933; font-weight: bold;}
.php.geshi_code .es_h {color: #000099; font-weight: bold;}
.php.geshi_code .br0 {color: #009900;}
.php.geshi_code .sy0 {color: #339933;}
.php.geshi_code .sy1 {color: #000000; font-weight: bold;}
.php.geshi_code .st0 {color: #0000ff;}
.php.geshi_code .st_h {color: #0000ff;}
.php.geshi_code .nu0 {color: #cc66cc;}
.php.geshi_code .nu8 {color: #208080;}
.php.geshi_code .nu12 {color: #208080;}
.php.geshi_code .nu19 {color:#800080;}
.php.geshi_code .me1 {color: #004000;}
.php.geshi_code .me2 {color: #004000;}
.php.geshi_code .re0 {color: #000088;}
.php.geshi_code span.xtra { display:block; }
кот, котопес, лига, мясо, рыба, сайты, супермен, чемпион. шашлык.
Подскажите почему так получается? Как исправить?
В любом случае спасибо за внимание!
сделал так:/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .php.geshi_code {font-family:monospace;} .php.geshi_code .imp {font-weight: bold; color: red;} .php.geshi_code .kw1 {color: #b1b100;} .php.geshi_code .kw2 {color: #000000; font-weight: bold;} .php.geshi_code .kw3 {color: #990000;} .php.geshi_code .co1 {color: #666666; font-style: italic;} .php.geshi_code .co2 {color: #666666; font-style: italic;} .php.geshi_code .co3 {color: #0000cc; font-style: italic;} .php.geshi_code .co4 {color: #009933; font-style: italic;} .php.geshi_code .coMULTI {color: #666666; font-style: italic;} .php.geshi_code .es0 {color: #000099; font-weight: bold;} .php.geshi_code .es1 {color: #000099; font-weight: bold;} .php.geshi_code .es2 {color: #660099; font-weight: bold;} .php.geshi_code .es3 {color: #660099; font-weight: bold;} .php.geshi_code .es4 {color: #006699; font-weight: bold;} .php.geshi_code .es5 {color: #006699; font-weight: bold; font-style: italic;} .php.geshi_code .es6 {color: #009933; font-weight: bold;} .php.geshi_code .es_h {color: #000099; font-weight: bold;} .php.geshi_code .br0 {color: #009900;} .php.geshi_code .sy0 {color: #339933;} .php.geshi_code .sy1 {color: #000000; font-weight: bold;} .php.geshi_code .st0 {color: #0000ff;} .php.geshi_code .st_h {color: #0000ff;} .php.geshi_code .nu0 {color: #cc66cc;} .php.geshi_code .nu8 {color: #208080;} .php.geshi_code .nu12 {color: #208080;} .php.geshi_code .nu19 {color:#800080;} .php.geshi_code .me1 {color: #004000;} .php.geshi_code .me2 {color: #004000;} .php.geshi_code .re0 {color: #000088;} .php.geshi_code span.xtra { display:block; }
<?foreach($tags as $row):?>
<?$end = end($tags);?>
<?$first = reset($tags);?>
<?if($first['id'] == $row['id']):?>
<a href='<?=base_url()?>note/search/<?=$row['id'];?>' class='tag_link'><?=mb_ucfirst($row['name']);?></a>,
<?elseif($end['id'] == $row['id']):?>
<a href='<?=base_url()?>note/search/<?=$row['id']?>' class='tag_link'><?=$row['name']?></a>.
<?else:?>
<a href='<?=base_url()?>note/search/<?=$row['id']?>' class='tag_link'><?=$row['name']?></a>,
<?endif?>
<?endforeach;?>
в итоге сделал: /** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .php.geshi_code {font-family:monospace;} .php.geshi_code .imp {font-weight: bold; color: red;} .php.geshi_code .kw1 {color: #b1b100;} .php.geshi_code .kw2 {color: #000000; font-weight: bold;} .php.geshi_code .kw3 {color: #990000;} .php.geshi_code .co1 {color: #666666; font-style: italic;} .php.geshi_code .co2 {color: #666666; font-style: italic;} .php.geshi_code .co3 {color: #0000cc; font-style: italic;} .php.geshi_code .co4 {color: #009933; font-style: italic;} .php.geshi_code .coMULTI {color: #666666; font-style: italic;} .php.geshi_code .es0 {color: #000099; font-weight: bold;} .php.geshi_code .es1 {color: #000099; font-weight: bold;} .php.geshi_code .es2 {color: #660099; font-weight: bold;} .php.geshi_code .es3 {color: #660099; font-weight: bold;} .php.geshi_code .es4 {color: #006699; font-weight: bold;} .php.geshi_code .es5 {color: #006699; font-weight: bold; font-style: italic;} .php.geshi_code .es6 {color: #009933; font-weight: bold;} .php.geshi_code .es_h {color: #000099; font-weight: bold;} .php.geshi_code .br0 {color: #009900;} .php.geshi_code .sy0 {color: #339933;} .php.geshi_code .sy1 {color: #000000; font-weight: bold;} .php.geshi_code .st0 {color: #0000ff;} .php.geshi_code .st_h {color: #0000ff;} .php.geshi_code .nu0 {color: #cc66cc;} .php.geshi_code .nu8 {color: #208080;} .php.geshi_code .nu12 {color: #208080;} .php.geshi_code .nu19 {color:#800080;} .php.geshi_code .me1 {color: #004000;} .php.geshi_code .me2 {color: #004000;} .php.geshi_code .re0 {color: #000088;} .php.geshi_code span.xtra { display:block; }
if ($posstags) {
foreach($posttags as $tag){
if(!next($posttags)){
echo $tag->name.".";
}else{
echo $tag->name.",";
}
}
}
/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .php.geshi_code {font-family:monospace;} .php.geshi_code .imp {font-weight: bold; color: red;} .php.geshi_code .kw1 {color: #b1b100;} .php.geshi_code .kw2 {color: #000000; font-weight: bold;} .php.geshi_code .kw3 {color: #990000;} .php.geshi_code .co1 {color: #666666; font-style: italic;} .php.geshi_code .co2 {color: #666666; font-style: italic;} .php.geshi_code .co3 {color: #0000cc; font-style: italic;} .php.geshi_code .co4 {color: #009933; font-style: italic;} .php.geshi_code .coMULTI {color: #666666; font-style: italic;} .php.geshi_code .es0 {color: #000099; font-weight: bold;} .php.geshi_code .es1 {color: #000099; font-weight: bold;} .php.geshi_code .es2 {color: #660099; font-weight: bold;} .php.geshi_code .es3 {color: #660099; font-weight: bold;} .php.geshi_code .es4 {color: #006699; font-weight: bold;} .php.geshi_code .es5 {color: #006699; font-weight: bold; font-style: italic;} .php.geshi_code .es6 {color: #009933; font-weight: bold;} .php.geshi_code .es_h {color: #000099; font-weight: bold;} .php.geshi_code .br0 {color: #009900;} .php.geshi_code .sy0 {color: #339933;} .php.geshi_code .sy1 {color: #000000; font-weight: bold;} .php.geshi_code .st0 {color: #0000ff;} .php.geshi_code .st_h {color: #0000ff;} .php.geshi_code .nu0 {color: #cc66cc;} .php.geshi_code .nu8 {color: #208080;} .php.geshi_code .nu12 {color: #208080;} .php.geshi_code .nu19 {color:#800080;} .php.geshi_code .me1 {color: #004000;} .php.geshi_code .me2 {color: #004000;} .php.geshi_code .re0 {color: #000088;} .php.geshi_code span.xtra { display:block; }
Array
(
[6] => stdClass Object
(
[term_id] => 6
[name] => кот
[slug] => %d0%ba%d0%be%d1%82
[term_group] => 0
[term_taxonomy_id] => 6
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[7] => stdClass Object
(
[term_id] => 7
[name] => котопес
[slug] => %d0%ba%d0%be%d1%82%d0%be%d0%bf%d0%b5%d1%81
[term_group] => 0
[term_taxonomy_id] => 7
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[9] => stdClass Object
(
[term_id] => 9
[name] => лига
[slug] => %d0%bb%d0%b8%d0%b3%d0%b0
[term_group] => 0
[term_taxonomy_id] => 9
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[4] => stdClass Object
(
[term_id] => 4
[name] => мясо
[slug] => %d0%bc%d1%8f%d1%81%d0%be
[term_group] => 0
[term_taxonomy_id] => 4
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[3] => stdClass Object
(
[term_id] => 3
[name] => рыба
[slug] => %d1%80%d1%8b%d0%b1%d0%b0
[term_group] => 0
[term_taxonomy_id] => 3
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[11] => stdClass Object
(
[term_id] => 11
[name] => сайты
[slug] => %d1%81%d0%b0%d0%b9%d1%82%d1%8b
[term_group] => 0
[term_taxonomy_id] => 11
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[8] => stdClass Object
(
[term_id] => 8
[name] => супермен
[slug] => %d1%81%d1%83%d0%bf%d0%b5%d1%80%d0%bc%d0%b5%d0%bd
[term_group] => 0
[term_taxonomy_id] => 8
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[10] => stdClass Object
(
[term_id] => 10
[name] => чемпион
[slug] => %d1%87%d0%b5%d0%bc%d0%bf%d0%b8%d0%be%d0%bd
[term_group] => 0
[term_taxonomy_id] => 10
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
[5] => stdClass Object
(
[term_id] => 5
[name] => шашлык
[slug] => %d1%88%d0%b0%d1%88%d0%bb%d1%8b%d0%ba
[term_group] => 0
[term_taxonomy_id] => 5
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[object_id] => 4
)
)
кот, котопес, лига, мясо, рыба, сайты, супермен, чемпион. шашлык.
В любом случае спасибо за внимание!
сделал так:/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .php.geshi_code {font-family:monospace;} .php.geshi_code .imp {font-weight: bold; color: red;} .php.geshi_code .kw1 {color: #b1b100;} .php.geshi_code .kw2 {color: #000000; font-weight: bold;} .php.geshi_code .kw3 {color: #990000;} .php.geshi_code .co1 {color: #666666; font-style: italic;} .php.geshi_code .co2 {color: #666666; font-style: italic;} .php.geshi_code .co3 {color: #0000cc; font-style: italic;} .php.geshi_code .co4 {color: #009933; font-style: italic;} .php.geshi_code .coMULTI {color: #666666; font-style: italic;} .php.geshi_code .es0 {color: #000099; font-weight: bold;} .php.geshi_code .es1 {color: #000099; font-weight: bold;} .php.geshi_code .es2 {color: #660099; font-weight: bold;} .php.geshi_code .es3 {color: #660099; font-weight: bold;} .php.geshi_code .es4 {color: #006699; font-weight: bold;} .php.geshi_code .es5 {color: #006699; font-weight: bold; font-style: italic;} .php.geshi_code .es6 {color: #009933; font-weight: bold;} .php.geshi_code .es_h {color: #000099; font-weight: bold;} .php.geshi_code .br0 {color: #009900;} .php.geshi_code .sy0 {color: #339933;} .php.geshi_code .sy1 {color: #000000; font-weight: bold;} .php.geshi_code .st0 {color: #0000ff;} .php.geshi_code .st_h {color: #0000ff;} .php.geshi_code .nu0 {color: #cc66cc;} .php.geshi_code .nu8 {color: #208080;} .php.geshi_code .nu12 {color: #208080;} .php.geshi_code .nu19 {color:#800080;} .php.geshi_code .me1 {color: #004000;} .php.geshi_code .me2 {color: #004000;} .php.geshi_code .re0 {color: #000088;} .php.geshi_code span.xtra { display:block; }
<?foreach($tags as $row):?>
<?$end = end($tags);?>
<?$first = reset($tags);?>
<?if($first['id'] == $row['id']):?>
<a href='<?=base_url()?>note/search/<?=$row['id'];?>' class='tag_link'><?=mb_ucfirst($row['name']);?></a>,
<?elseif($end['id'] == $row['id']):?>
<a href='<?=base_url()?>note/search/<?=$row['id']?>' class='tag_link'><?=$row['name']?></a>.
<?else:?>
<a href='<?=base_url()?>note/search/<?=$row['id']?>' class='tag_link'><?=$row['name']?></a>,
<?endif?>
<?endforeach;?>
в итоге сделал: /** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .php.geshi_code {font-family:monospace;} .php.geshi_code .imp {font-weight: bold; color: red;} .php.geshi_code .kw1 {color: #b1b100;} .php.geshi_code .kw2 {color: #000000; font-weight: bold;} .php.geshi_code .kw3 {color: #990000;} .php.geshi_code .co1 {color: #666666; font-style: italic;} .php.geshi_code .co2 {color: #666666; font-style: italic;} .php.geshi_code .co3 {color: #0000cc; font-style: italic;} .php.geshi_code .co4 {color: #009933; font-style: italic;} .php.geshi_code .coMULTI {color: #666666; font-style: italic;} .php.geshi_code .es0 {color: #000099; font-weight: bold;} .php.geshi_code .es1 {color: #000099; font-weight: bold;} .php.geshi_code .es2 {color: #660099; font-weight: bold;} .php.geshi_code .es3 {color: #660099; font-weight: bold;} .php.geshi_code .es4 {color: #006699; font-weight: bold;} .php.geshi_code .es5 {color: #006699; font-weight: bold; font-style: italic;} .php.geshi_code .es6 {color: #009933; font-weight: bold;} .php.geshi_code .es_h {color: #000099; font-weight: bold;} .php.geshi_code .br0 {color: #009900;} .php.geshi_code .sy0 {color: #339933;} .php.geshi_code .sy1 {color: #000000; font-weight: bold;} .php.geshi_code .st0 {color: #0000ff;} .php.geshi_code .st_h {color: #0000ff;} .php.geshi_code .nu0 {color: #cc66cc;} .php.geshi_code .nu8 {color: #208080;} .php.geshi_code .nu12 {color: #208080;} .php.geshi_code .nu19 {color:#800080;} .php.geshi_code .me1 {color: #004000;} .php.geshi_code .me2 {color: #004000;} .php.geshi_code .re0 {color: #000088;} .php.geshi_code span.xtra { display:block; }
1 ответов
Функция next(); перемещает внутренний указатель массива на одну позицию вперёд перед тем, как вернуть значение. Цикл foreach тоже перемещает указатель на каждой итерации.
Я думаю, нужно одно из двух устранить. Либо перепишите цикл на while, либо уберите next.