从数组中获取值

时间:2017-01-09 07:08:32

标签: php arrays

我想从数组中获取'video_url'值。我有数组

[field_read_link] => Array ( 
  [und] => Array ( 

     [0] => Array ( 
       [url] => http://localhost/AspireInnerWebsite/ 
       [title] => Read [attributes] => Array ( ) 
     ) 

   ) 
 ) 
 [field_perspective_image] => Array ( ) 
 [field_fb_icon] => Array ( ) 
 [field_twitter_icon] => Array ( ) 
 [field_gplus_icon] => Array ( ) 
 [field_service_line] => Array ( 
   [und] => Array ( 

     [0] => Array ( 
       [tid] => 19 
     ) 

    ) 
  ) 
  [field_category] => Array ( 
    [und] => Array ( 

      [0] => Array ( 
        [tid] => 31 
      ) 

     ) 
   ) 
   [field_perspective_menu] => Array ( 
     [und] => Array ( 

       [0] => Array ( 
         [tid] => 44 
       ) 

      ) 
    ) 
    [field_display_order] => Array ( 
      [und] => Array ( 

        [0] => Array ( 
          [value] => 1 
        ) 

       ) 
     ) 
     [field_attachment_file] => Array ( ) 
     [field_perspective_video] => Array ( 
       [und] => Array ( 

       [0] => Array ( 
         [video_url] => https://www.youtube.com/embed/vG-5jZpMIHk       
         [thumbnail_path] => public://video_embed_field_thumbnails/youtube/vG-5jZpMIHk.jpg 
         [video_data] => a:1:{s:7:"handler";s:7:"youtube";}  
         [embed_code] =>     
 [description] => ) ) ) [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] => 1483523222 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => nivetha [picture] => 0 [data] => a:1:{s:7:"overlay";i:1;} ) ) )`
从该数组

我想获取field_perspective_video

中的'video_url'

我试过

print_r($fields['field_perspective_video'][0]['video_url']);

我抓住了

  

不能使用stdClass类型的对象作为行

中的数组

2 个答案:

答案 0 :(得分:0)

这意味着您的$fields是stdClass(对象)而不是数组。

如果它来自json_decode,请使用json_decode($string, true)解码为数组而不是对象。

否则尝试使用对象表示法$fields->field_perspective_video[0]->video_url

答案 1 :(得分:0)

它看起来像stdClass(对象)所以得到这样的

$fields->field_perspective_video['und'][0]['video_url'];