2

I'm learning PHP and Drupal. I need to reference a variable contained in an array called $contexts.

So print_r($contexts) gives me this:

Array ( 
   [context_view_1] => ctools_context Object ( 
       [type] => view 
       [view] => view Object ( 
          [db_table] => views_view 
          [result] => Array ( 
              [0] => stdClass Object ( 
                   [nid] => 28 
                   [node_data_field_display_field_display_value] => slideshow
              ) 
           )

eek confusing. I want to work with the node_data_field_display_field_display_value variable. I think my code needs to be like this, but I know this isn't right:

if ($contexts['context_view_1']['view']['result'][0]
['node_data_field_display_field_display_value'] == 'slideshow') then do whatever...

Thanks!

1
  • Try this out : $contexts['context_view_1']->view->result[0]->node_data_field_display_field_display_value Commented Mar 9, 2011 at 17:08

6 Answers 6

5

You suggested the following array reference to get to the variable you want:

 $contexts['context_view_1']['view']['result'][0]['node_data_field_display_field_display_value']

The reason this doesn't work is because some of the structures in the chain are actually objects rather than arrays, so you need a different syntax for them to get at their properties.

So the first layer is correct, because $contexts is an array, so context_view_1 is an array element, so you'd get to it with $contexts['context_view_1'] as you did.

But the next level is an object, so to get to view, you need to reference it as an object property with -> syntax, like so: $contexts['context_view_1']->view

For each level down the tree, you need to determine whether it's an object or an array element, and use the correct syntax.

In this case, you'll end up with something that looks like this:

$context['context_view_1']->view->result[0]->node_data_field_display_field_display_value
Sign up to request clarification or add additional context in comments.

Comments

4

That's a mess of a variable. The issue you're having is that you're using the bracketed notation, e.g. "['view']", for each "step" in the navigation through your variable. That would be fine if each child of the variable were an array, but not every one is.

You'll note, for example, that $contexts['context_view_1'] is actually an object, not an array (take note that it says "[context_view_1] => ctools_context Object"). Whereas you would use that bracketed notation to address the elements of an array, you use the arrow operator to address the properties of an object.

Thus, you would address the field you are trying to reach with the following expression:

$contexts['context_view_1']->view->result[0]->node_data_field_display_field_display_value

1 Comment

+1 for "that's a mess of a variable", but Drupal does have a habit of giving you messy structures like this. :-/
1

For properties listed as "Object", you need to use -> to get into it, and "Array", you need to use []. So:

$contexts['context_view_1']->view->result[0]->node_data_field_display_field_display_value

Comments

0
$contexts['context_view_1']->view->result[0]->node_data_field_display_field_display_value

Comments

0
echo $context['context_view_1']->view->result[0]->node_data_field_display_field_display_value;

Comments

0

Do not mistake objects with arrays. A memeber of an array can be accesed by $array['member'], but fields of an object can be accessed as $object->fieldname.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.