-2

Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code

I've got a complex array variable called $data. I need to pull out one very buried value: [url] in [field_website]. Here's the raw print_r() output of the $data variable:

stdClass Object ( [node_title] => CMI2 [nid] => 3 [field_data_field_website_node_entity_type] => node [field_data_field_blog_node_entity_type] => node [field_data_field_rss_node_entity_type] => node [field_data_field_twitter_node_entity_type] => node [field_data_field_yammer_node_entity_type] => node [field_data_field_facebook_node_entity_type] => node [field_data_field_flickr_node_entity_type] => node [field_data_field_youtube_node_entity_type] => node [_field_data] => Array ( [nid] => Array ( [entity_type] => node [entity] => stdClass Object ( [vid] => 3 [uid] => 1 [title] => CMI2 [log] => [status] => 1 [comment] => 1 [promote] => 0 [sticky] => 0 [nid] => 3 [type] => social_source [language] => und [created] => 1356040541 [changed] => 1356040541 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1356040541 [revision_uid] => 1 [field_website] => Array ( [und] => Array ( [0] => Array ( [url] => http://cmi2.yale.edu [title] => [attributes] => Array ( ) ) ) ) [field_blog] => Array ( ) [field_rss] => Array ( ) [field_twitter] => Array ( [und] => Array ( [0] => Array ( [url] => http://twitter.com/yalecmi2 [title] => [attributes] => Array ( ) ) ) ) [field_facebook] => Array ( ) [field_youtube] => Array ( ) [field_flickr] => Array ( ) [field_yammer] => Array ( ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item [1] => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date [1] => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1356040541 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => admin [picture] => 0 [data] => b:0; ) ) ) [field_field_website] => Array ( [0] => Array ( [rendered] => Array ( [#markup] => http://cmi2.yale.edu [#access] => 1 ) [raw] => Array ( [url] => http://cmi2.yale.edu [title] => http://cmi2.yale.edu [attributes] => Array ( ) [display_url] => http://cmi2.yale.edu ) ) ) [field_field_blog] => Array ( ) [field_field_rss] => Array ( ) [field_field_twitter] => Array ( [0] => Array ( [rendered] => Array ( [#markup] => http://twitter.com/yalecmi2 [#access] => 1 ) [raw] => Array ( [url] => http://twitter.com/yalecmi2 [title] => http://twitter.com/yalecmi2 [attributes] => Array ( ) [display_url] => http://twitter.com/yalecmi2 ) ) ) [field_field_yammer] => Array ( ) [field_field_facebook] => Array ( ) [field_field_flickr] => Array ( ) [field_field_youtube] => Array ( ) )

(sorry about that ugliness!)

How the heck do I pull out that [url] variable?! Ideally, I just want to assign that one value to another variable or just print it out.

If it's helpful, this is from a Drupal 7 view with the Views PHP module.

Thanks!

Community
  • 1
  • 1
Sam
  • 2,152
  • 6
  • 31
  • 44
  • 3
    Quick tip, when you print out a array/object, wrap it in `
    ` tags, it will format it nicely.
    – Supericy Dec 23 '12 at 04:04
  • 3
    Also, use var_export, not print_r. Then we can take your output and use it in code of our own as we try to figure out your questions. – DWright Dec 23 '12 at 04:06
  • Go through the array? `$data[primaryarraykey][secondaryarray][..][..]` Keep going until you get to the actual key you want? – Daryl Gill Dec 23 '12 at 06:23

1 Answers1

1

I recommend reading up about PHP arrays and objects since what you are trying to do is so trivial.

http://php.net/manual/en/language.types.array.php

http://php.net/manual/en/sdo.sample.getset.php

Supericy
  • 5,866
  • 1
  • 21
  • 25
  • I have read through the PHP resources and I've found nothing of value. Could you point to a specific section? Thanks! – Sam Dec 23 '12 at 05:08
  • What URL do you want from your print_r output (there are a few)? If you provide me with the one you want, I can give you an example of how to access it. – Supericy Dec 23 '12 at 05:12
  • This is basically the path to the URL that I need... but if I can just get a sense of the syntax, I can probably take it from there. Thanks for your help! `['_field_data']['nid']['entity']['field_website']['und']['0']['url']` – Sam Dec 23 '12 at 05:22
  • You are close. The `$data` variable is an object as well as the entity, so you have to use the `->` operator to access their properties. `$data->_field_data['nid']['entity']->field_website['und'][0]['url'];` – Supericy Dec 23 '12 at 05:26