简体   繁体   中英

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. This is the output of a WordPress Function 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'. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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