[taxonomy] => Array
        (
            [27] => stdClass Object
                (
                    [tid] => 27
                    [vid] => 3
                    [name] => handheld
                    [description] => 
                    [weight] => 0
                )

            [13] => stdClass Object
                (
                    [tid] => 13
                    [vid] => 7
                    [name] => Technology
                    [description] => 
                    [weight] => 0
                )

            [9] => stdClass Object
                (
                    [tid] => 9
                    [vid] => 2
                    [name] => Phone
                    [description] => 
                    [weight] => 0
                )

            [11] => stdClass Object
                (
                    [tid] => 11
                    [vid] => 4
                    [name] => Nokia
                    [description] => 
                    [weight] => 0
                )

            [20] => stdClass Object
                (
                    [tid] => 20
                    [vid] => 8
                    [name] => Current
                    [description] => Object is the up-to-date version.
                    [weight] => 0
                )

        )

I want to grab the 'Brand' or fourth name in each node that will contain these terms. What's the best way to loop through the taxonomy array to get to the fourth value each time?

<?php print $node->taxonomy[10]->name;?>

Will display 'Nokia', but I need the value of the brand everytime, not just term 11's name.
Cheers, hope I have explained this alright.

Comments

joachim’s picture

foreach ($node->taxonomy as $tid) {
print $tid->name;
}

Fixdit’s picture

Thanks for the reply joahim, however I required just the fourth value.

Fixdit’s picture

Silly me, got it...

<?php
foreach ($node->taxonomy as $tid) {
	$array[] = $tid->name;
}
	print $array[3];
?>

Pro Drupal Development from London
ashleydavison [at] enjoystudios.co.uk

gforce301’s picture

There may be some conditions that might change the order of the taxonomy array in the node. I never trust an unordered array. That being said you could look for the vocabulary id to make sure you grab the right term for the "brand". For example:

<?php
foreach ($node->taxonomy as $tid) {
    if ($tid->vid == 4) // <-- the vocabulary id of the "brand"
        $term_name = $tid->name;
}
    print $term_name;
?>
marcelodornelas’s picture

Thanks Brian, I made that mistake. It's a good idea to use the vid. Cheers.

joachim’s picture

Sorry, late at night and didn't catch the bit about the fourth one... :)

HunterElliott’s picture

Joachim, just wanted to thank you for this bit of code. I was tearing what little hair I have left out of my head trying to display a node page that I wanted to embed the results of the taxonomy for that node. I couldn't just link to the results of the taxonomy as I wanted other node-specific info on the page as well. I had a View that grabbed all the correct taxonomy results, but I needed to pass that taxonomy ID to it.

For anyone else needing the ID of the taxonomy item and not the name to pass it in an embedded view for a node, here's what I used:

      <?php  
          $viewName = 'taxonomy_term_node';
  	  $display_id = 'default';
          foreach ($node->taxonomy as $tax) 
          print views_embed_view($viewName, $display_id, $tax->tid);  
      ?>