0

I have some content types (nodes) that are attached to various taxonomies. For specific node types, I want to do some validation on the taxonomy. I do not want to hard-code the nodes types and their corresponding fields that reference the taxonomy. So I put them in array. However, I am unable to dereference the field names. I've tried double $$, quotes, etc, but can't get it to work. Is what I want to do possible?

<?php

$node = (object) array(
    'nid' => NULL,
    'vid' => NULL,
    'uid' => '1',
    'type' => 'price_document',
    'language' => 'und',
    'field_taxonomy_price' => array(
        'und' => array(
            array(
                'tid' => '94'
            )
        )
    ),
);


  $nodes_to_check = array("price_document" => "field_taxonomy_price",
                          "package"        => "field_taxonomy_package",
                         );



  if (array_key_exists($node->type,$nodes_to_check)) {
    $taxonomy_field = $nodes_to_check[$node->type];
    print_r($taxonomy_field);
    $tid = $node->field_taxonomy_price ['und'][0]['tid']; //  <- this works but, how
    //$tid = $node->"$$taxonomy_field" ['und'][0]['tid'];     <- can I deref variable?
  }
?>

1 Answer 1

0

You just need to use curly braces; there's no need for variable variables in this case:

$tid = $node->{$taxonomy_field}['und'][0]['tid'];

Your Answer

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

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