首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对php中的对象的迭代不能正常工作。

对php中的对象的迭代不能正常工作。
EN

Stack Overflow用户
提问于 2014-02-19 21:53:28
回答 1查看 30关注 0票数 0

我一直在努力让php中的对象数组的迭代工作。我有这样的数组结构:

代码语言:javascript
复制
Array
(

[0] => stdClass Object

(
        [tid] => 7
        [vid] => 2
        [name] => Bakkerijen
        [description] => 
        [format] => full_html
        [weight] => 0
        [depth] => 0
        [parents] => Array
            (
                [0] => 0
            )

    )

[1] => stdClass Object
    (
        [tid] => 10
        [vid] => 2
        [name] => Horeca
        [description] => 
        [format] => full_html
        [weight] => 1
        [depth] => 0
        [parents] => Array
            (
                [0] => 0
            )

    )

[2] => stdClass Object
    (
        [tid] => 8
        [vid] => 2
        [name] => Food Shops
        [description] => 
        [format] => full_html
        [weight] => 2
        [depth] => 0
        [parents] => Array
            (
                [0] => 0
            )

    )   

)

如果一个值与这个数组中的tid匹配,我想向li添加一个选定的类。

下面是我所做的代码和HTML:

代码语言:javascript
复制
 <?php $current_tid = 7; ?>
 <?php foreach ($terms as $term): ?>
              <?php unset($term->parents); ?> 
                <?php if($term->tid == $current_tid) { ?>
                <li><a href="/portfolio#filter=.tid-<?php print $term->tid; ?>" class="selected"><?php print $term->name;?></li>
                <?php }else if($term->tid != $current_tid){ ?>
                <li><a href="/portfolio#filter=.tid-<?php print $term->tid; ?>"><?php print $term->name; ?></a></li>

                <?php } ?>
              <?php endforeach; ?> 

这是我得到的最后一个输出:

代码语言:javascript
复制
<ul class="cathome-list-inner">                            
<li>
    <a class="selected" href="/portfolio#filter=.tid-7">Bakkerijen</a>
</li>
<a class="selected" href="/portfolio#filter=.tid-7"></a>
<li>
    <a class="selected" href="/portfolio#filter=.tid-7"></a>
    <a href="/portfolio#filter=.tid-10">Horeca</a>
</li>                                          
<li>
    <a href="/portfolio#filter=.tid-8">Food Shops</a></li>
<li>
    <a href="/portfolio#filter=.tid-9">Non-food Shops</a></li>
<li>
    <a href="/portfolio#filter=.tid-11">Privé interieurprojecten</a>
</li>                                                   

问题是为什么我要在所选的类中添加额外的锚标记。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-19 21:59:23

在这条线上:

代码语言:javascript
复制
<li><a href="/portfolio#filter=.tid-<?php print $term->tid; ?>" class="selected"><?php print $term->name;?></li>

您正在丢失关闭的</a>浏览器可能试图自动更正您的错误。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21893309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档