PHP Object array trials

This week I had a bit of a struggle figuring out how to access a value in a PHP array with stdClass Objects all nested together. It was the output from a drupal view I wanted to use a node reference field value as a context filter on another view. Long story.

I used a temporary page.tpl for that specific page to work out how to get to the php I needed for the contextual filter.

$myVar1 = views_get_view_result('current_session', 'block_1'); // get the output from the view and set the array object to $myVar1

I'm not fully understanding yet why but for some reason when it comes to node reference fields you get way more than you were hoping for in your output.

Array ( [0] => stdClass Object ( [nid] => 40436 [node_created] => 1454437482 [field_data_field_session_node_entity_type] => node [_field_data] => Array ( [nid] => Array ( [entity_type] => node [entity] => stdClass Object ( [vid] => 60381 [uid] => 887 [title] => redacted [log] => Created by redacted. [status] => 1 [comment] => 1 [promote] => 0 [sticky] => 0 [ds_switch] => [nid] => 40436 [type] => redacted [language] => und [created] => 1454437482 [changed] => 1454437482 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1454437482 [revision_uid] => 887 [field_summary_title] => Array ( [und] => Array ( [0] => Array ( [value] => redacted [format] => [safe_value] => redacted ) ) ) [body] => Array ( ) [field_department] => Array ( [und] => Array ( [0] => Array ( [tid] => 15 ) ) ) [field_rstatus] => Array ( [und] => Array ( [0] => Array ( [value] => pending ) ) ) [field_resolution_pdf] => Array ( [und] => Array ( [0] => Array ( [fid] => 24806 [uid] => 887 [filename] => redacted.pdf [uri] => public://redacted.pdf [filemime] => application/pdf [filesize] => 70324 [status] => 1 [timestamp] => 1454437482 [type] => default [rdf_mapping] => Array ( ) [display] => 1 [description] => ) ) ) [field_in_committee] => Array ( [und] => Array ( [0] => Array ( [nid] => 24800 ) [1] => Array ( [nid] => 24802 ) [2] => Array ( [nid] => 25128 ) [3] => Array ( [nid] => 24804 ) ) ) [field_adopted_by_the_following] => Array ( ) [field_sponsors] => Array ( [und] => Array ( [0] => Array ( [nid] => 24788 ) ) ) [field_term] => Array ( [und] => Array ( [0] => Array ( [value] => 2016 ) ) ) [field_supporting_documents] => Array ( ) [field_co_sponsors] => Array ( ) [field_flickr_date] => Array ( ) [field_redacted] => Array ( [und] => Array ( [0] => Array ( [value] => 2014-2015 ) ) ) [field_session] => Array ( [und] => Array ( [0] => Array ( [nid] => 40360 ) ) ) [field_inheritance] => Array ( ) [field_parent] => Array ( )...etc

It literally gets dumped like that with no readability built in. In order to figure out what I was working with I dumped it into a text editor and by hand added line breaks so I could figure out where I was.

Array ( 
	[0] => stdClass Object ( 
		[nid] => 40436 
		[node_created] => 1454437482 
		[field_data_field_session_node_entity_type] => node 
		[_field_data] => Array ( 
			[nid] => Array ( 
				[entity_type] => node 
				[entity] => stdClass Object ( 
					[vid] => 60381 
					[uid] => 887 
					[title] => redacted 
					[log] => Created by redacted. 
					[status] => 1 
					[comment] => 1 
					[promote] => 0 
					[sticky] => 0 
					[ds_switch] => 
					[nid] => 40436 
					[type] => redacted 
					[language] => und 
					[created] => 1454437482 
					[changed] => 1454437482 
					[tnid] => 0 [translate] => 0 
					[revision_timestamp] => 1454437482 
					[revision_uid] => 887 
					[field_summary_title] => Array ( 
						[und] => Array ( 
							[0] => Array ( 
								[value] => redacted 
								[format] => 
								[safe_value] => redacted ) ) ) 
					[body] => Array ( ) 
					[field_department] => Array ( 
						[und] => Array ( 
							[0] => Array ( 
								[tid] => 15 ) ) ) 
					[field_rstatus] => Array ( 
						[und] => Array ( 
							[0] => Array ( 
								[value] => pending ) ) ) 	
								
					...
					
					[field_session] => Array ( 
						[und] => Array ( 
							[0] => Array ( 
								[nid] => 40360 ) ) ) 
					[field_inheritance] => Array ( ) 
					[field_parent] => Array ( ) 
					[field_revision_vote] => Array ( ) ... etc.

All that and I just wanted to get the field_session nid value. I struggled with the view for awhile trying to get it to only give me what I want but it was no good. Something about using Node References gives it the blurts. I sort of understand Objects and Arrays from javascript but this is a very different animal. How I figured it out was by doing a print_r starting from the root variable then working through piece by piece:

 

print_r $myVar1; //outputs the entire object
$myVar1[0]->_field_data... $myVar1[0]->_field_data[nid]... $myVar1[0]->_field_data[nid][entity]... 

And so on until I got what I wanted. The trick for me was learning the use of the

 -> 

vs. the

 [ ]

operators. I'm not sure where on Stackexchange I found the answer but it was a post very similar to this one: http://stackoverflow.com/questions/5156841/stdclass-object-and-array-how...

The final solution was this:

$sesVal1 = $myVar1[0]->_field_data[nid][entity]->field_session[und][0][nid];

Which translates to (usable variable)$sesVal1 = (array variable)$myVar1 (first in the index)[0] (object) -> _field_data(array)[nid](array)[entity](object)->field_session(array)[und](first in index)[0](array)[nid] (the value!)

Something like that. It only took me 4 hours to figure out but I think it will help a lot with my understanding of what's going on in drupal with accessing arrays and objects. It goes a long way from copy/pasting solutions and thinking 'well it works, whatever it does' and getting beyond cargo cult voodoo coding.

I hope so anyway.