检查关联数组是否具有特定值

时间:2016-12-13 21:57:42

标签: php wordpress

我有一个关联数组,其中包含一个WordPress帖子中的标签列表。这是WordPress函数的输出 wp_get_post_tags

array(3) { [0]=> object(WP_Term)#300 (10) { ["term_id"]=> int(22) ["name"]=> string(10) "Case Study" ["slug"]=> string(10) "case-study" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(22) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(6) ["filter"]=> string(3) "raw" } [1]=> object(WP_Term)#295 (10) { ["term_id"]=> int(9) ["name"]=> string(9) "Microsoft" ["slug"]=> string(9) "microsoft" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(9) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(11) ["filter"]=> string(3) "raw" } [2]=> object(WP_Term)#367 (10) { ["term_id"]=> int(27) ["name"]=> string(10) "SharePoint" ["slug"]=> string(10) "sharepoint" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(27) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(10) ["filter"]=> string(3) "raw" } } 

我想检查它是否包含标签名称' SharePoint'。我经历了各种资源,但找不到办法。这是Array

的结构
Array
(
   [0] => stdClass Object
       (
           [term_id] => 4
           [name] => tag2
           [slug] => tag2
           [term_group] => 0
           [term_taxonomy_id] => 4
           [taxonomy] => post_tag
           [description] => 
           [parent] => 0
           [count] => 7
       )

   [1] => stdClass Object
       (
           [term_id] => 7
           [name] => tag5
           [slug] => tag5
           [term_group] => 0
           [term_taxonomy_id] => 7
           [taxonomy] => post_tag
           [description] => 
           [parent] => 0
           [count] => 6
       )

   [2] => stdClass Object
       (
           [term_id] => 16
           [name] => tag6
           [slug] => tag6
           [term_group] => 0
           [term_taxonomy_id] => 16
           [taxonomy] => post_tag
           [description] => 
           [parent] => 0
           [count] => 2
       )

)

我尝试过这样做,但它没有工作

//$a is the Array
foreach ($a as $key=>$value) {
if ($value=='SharePoint'){
echo "True"
}

请帮助。

1 个答案:

答案 0 :(得分:2)

你很接近,但不要忘记$ value是一个对象,所以你的代码应该这样调整:

foreach ($a as $key=>$value) {
if ($value->name=='SharePoint'){
echo "True";
}