简体   繁体   English

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

[英]To check whether an Associative Array has got a particular value

I have an Associative array which contains a list of tags from a WordPress Post. 我有一个关联数组,其中包含来自WordPress帖子的标签列表。 This is the output of a WordPress Function wp_get_post_tags 这是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" } } 

and I want to check whether it contains the tag Name 'SharePoint'. 我想检查它是否包含标签名称'SharePoint'。 I went through various resources but cannot find a way to do it. 我经历了各种资源,但找不到解决方法。 This is the structure of the 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
       )

)

I tried doing this but it didn't work 我尝试这样做,但是没有用

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

Kindly Help. 请帮助。

You was close but don't forget $value is an object, so your code should be adjusted this way: 您很亲近,但不要忘记$ value是一个对象,因此应通过以下方式调整代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM