2012-08-22 101 views
0
<?php // SEARCH TAGS FOR A SPECIFIC TAG 
    $tags = CollectionAttributeKey::getByHandle('recipe_tags'); // attribute handle of tags to search 
    $tagToFind = 'Videos'; // declare the specific tag to find 
    $selectedTags = array($page->getAttribute($tags->getAttributeKeyHandle())); // get tags associated with each page and put them in an array 

    foreach ($selectedTags as $tag) { // output tags separately 

    echo $tag; // ECHO TEST - check that individual tags associated with each page are outputting correctly 

    if ($tag == $tagToFind) { // if $tag is equal to $tagToFind 
     echo ' Found'; 
    } else { 
     echo ' Not found'; 
     } 
    } 
?> 

echo $tag;输出的每一页相关的标签列表,所以我敢肯定的问题是我如何检查,如果“视频”是在标签的列表。PHP - 搜索的数组,字符串

上述输出以下列表:BreakfastBrunchBudgetMealEasyLunchQuick MealsSupperVegetarianVideosNot found即使画是在列表中。

我也使用in_array寻找这样的“视频”的尝试:

if (in_array($tagToFind, $selectedTags, true)) { // search the array for $tagToFind - true = strict 
    echo ' Found'; 
} else { 
    echo ' Not found'; 
    } 

但得到相同的结果 - 我新的PHP很抱歉,如果这是很容易。

任何帮助将不胜感激。

干杯

回答

0
$page->getAttribute($tags->getAttributeKeyHandle()) 

似乎返回为特林。

在这种情况下

$selectedTags = array($page->getAttribute($tags->getAttributeKeyHandle())); 

是没有意义的 - 你得到包含一个长字符串数组。

你想做什么的是:

$ selectedTags = $页面级>的getAttribute($ tags-> getAttributeKeyHandle());

if(stristr($selectedTags, $tagToFind)){ 
    // do something 
} 
else{ 
    // do something else 
} 
+0

真棒 - 容易,当你知道如何! – CMSCSS

3

似乎$ selectedTags一个字符串数组,因为你foreach只循环一次

你应该尝试做

$selectedTags = explode(" ",$page->getAttribute($tags->getAttributeKeyHandle())); 

然后用in_array功能

0

话,最好使用....

if(strcmp($tag , $tagToFind)) 
{ 
    echo "Found"; 
} 
else 
{ 
    echo "Not found"; 
} 

可能这对你的作品