0
我一直在努力獲得php中的對象數組的迭代工作。我有一個陣列結構是這樣的:迭代在php中的對象無法正常工作
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。
下面是代碼和HTML我做的是:
<?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; ?>
這是最後的輸出我得到:
<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>
問題是爲什麼我收到額外的添加選定類別的定位標記..
謝謝隊友..我不敢相信我犯了這樣一個愚蠢的錯誤:( –
適應我們最好的(也是最糟糕的)。 – Pitchinnate