1

I have an article that I've set to a variable like this:

$this_node = node_load($nid);

I've done a print_r on $this_node and this is what I get - it's pretty long, sorry:

stdClass Object
(
    [nid] => 67572
    [type] => imagevault
    [language] => 
    [uid] => 3
    [status] => 1
    [created] => 1303347600
    [changed] => 1305662899
    [comment] => 0
    [promote] => 0
    [moderate] => 0
    [sticky] => 1
    [tnid] => 0
    [translate] => 0
    [vid] => 67572
    [revision_uid] => 1
    [title] => Anne Jones
    [body] => 
    [teaser] => 
    [log] => 
    [revision_timestamp] => 1305662899
    [format] => 2
    [name] => pbreen
    [picture] => 
    [data] => a:0:{}
    [field_pub_date] => Array
        (
            [0] => Array
                (
                    [value] => 
                    [timezone] => America/Chicago
                    [timezone_db] => America/Chicago
                    [date_type] => date
                )

        )

    [field_att_file] => Array
        (
            [0] => Array
                (
                    [fid] => 59327
                    [uid] => 3
                    [filename] => KCJones.jpg
                    [filepath] => sites/default/files/images/KCJones.jpg
                    [filemime] => image/jpeg
                    [filesize] => 54806
                    [status] => 1
                    [timestamp] => 1304023490
                    [list] => 1
                    [data] => Array
                        (
                            [description] => 
                        )

                )

        )

    [path] => imagevault/67572


    [0] => 
    [taxonomy] => Array
        (
            [264] => stdClass Object
                (
                    [tid] => 264
                    [vid] => 15
                    [name] => Kimberly-Clark
                    [description] => 
                    [weight] => 0
                )

            [137] => stdClass Object
                (
                    [tid] => 137
                    [vid] => 10
                    [name] => People
                    [description] => 
                    [weight] => 0
                )

        )

)

I know I can get certain values from the node like this:

$imagefile = array();
$imagefile['name'] = $this_node->field_att_file[0]['filename'];

Here's what I need to do, though (and I'm very new to Drupal so forgive me if this is really simple.) The article can, as shown, have multiple taxonomy terms. I need to treat a chunk of display code differently if the article has 137 as one of the taxonomy terms. How would I go about this?

Something like this, or is there a better way? (Don't even know if this way works yet; it probably won't since each tax term is an object, not an array.)

$this_tax = $this_node->taxonomy;
if (array_key_exists('137', $this_tax)) {
   // Do stuff
}
2
  • 1
    Have you tried it? Something like that should actually work. The only issue might be that you are looking for the string '127' but want the integer 127, so remove the '. But I'm not 100% sure right now how PHP handles that. Also note that you can use isset() instead of array_key_exists() here, so just if (isset($this_node->taxonomy[137])) { ... should work too.
    – Berdir
    May 19, 2011 at 17:32
  • Whaddaya know? It did work! Thanks.
    – EmmyS
    May 19, 2011 at 19:39

1 Answer 1

2

Something like that should actually work.

The only issue might be that you are looking for the string '127' but want the integer 127, so remove the '. But I'm not 100% sure right now how PHP handles that.

Also note that you can use isset() instead of array_key_exists() here, so just

if (isset($this_node->taxonomy[137])) {
}

should work too. The difference between isset and array_key_exists() is that isset() returns FALSE if they key exists but the value of it is NULL, while array_key_exists() returns TRUE in that case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.