2013-01-17 105 views
-2

我試圖在Drupal 7安裝中打印值爲「Viviana」的值,這個值在配置文件節點內的某處。我想要的是打印輸出用戶名的字段內容。如何打印其他數組內的數組內的內容?

從來就試過這個選項,但從來就得到了錯誤:print $field_nombrecompleto1['und'][0]['value'];

The error was Notice: Undefined variable: field_nombrecompleto1 in /home/liga/public_html/sites/all/themes/liga/templates/user-profile.tpl.php on line 7 

I'm使用print_r的發佈後的輸出(一點點縮短)()。

感謝您的幫助!在你的輸出

Array 
(
    [template_file] => sites/all/themes/liga/templates/user-profile.tpl.php 
    [variables] => Array 
     (
      [elements] => Array 
       (
        [#pre_render] => Array 
         (
          [0] => _field_extra_fields_pre_render 
          [1] => field_group_build_pre_render 
         ) 

        [#entity_type] => user 
        [#bundle] => user 
        [#groups] => Array 
         (
         ) 

        [#fieldgroups] => Array 
         (
         ) 

        [#group_children] => Array 
         (
         ) 

        [#theme] => user_profile 
        [#account] => stdClass Object 
         (
          [uid] => 78 
          [name] => milne 
          [pass] => U$S$9B.hwxhcWeayArrP8Y/qsNXj9SNMh6z5FaCjAebOq1UrYbQJ/uIR 
          [mail] => [email protected] 
          [theme] => 
          [signature] => 
          [signature_format] => 3 
          [created] => 1157654988 
          [access] => 1185380339 
          [login] => 1185380321 
          [status] => 1 
          [timezone] => 
          [language] => es 
          [picture] => 
          [init] => [email protected] 
           ... 
           ... 
           ... 
        [#view_mode] => full 
        [#language] => es 
        [#block] => stdClass Object 
         (
          [module] => system 
          [delta] => main 
          [theme] => liga 
          [status] => 1 
          [weight] => -53 
          [region] => content 
          [custom] => 0 
          [visibility] => 0 
          [pages] => 
          [title] => 
          [bid] => 2154 
          [cache] => -1 
          [subject] => 
         ) 

        [#weight] => -53 
        [#theme_wrappers] => Array 
         (
          [0] => block 
         ) 
         ... 
         ... 
         ... 


        [profile_alumnos] => Array 
         (
          [#type] => user_profile_category 
          [#title] => Perfil Alumnos 
          [#prefix] => 
          [view] => Array 
           (
            [profile2] => Array 
             (
              [5] => Array 
               (
                [field_nombrecompleto1] => Array 
                 (
                  [#theme] => field 
                  [#weight] => 0 
                  [#title] => nombrecompleto1 
                  [#access] => 1 
                  [#label_display] => hidden 
                  [#view_mode] => account 
                  [#language] => und 
                  [#field_name] => field_nombrecompleto1 
                  [#field_type] => text 
                  [#field_translatable] => 0 
                  [#entity_type] => profile2 
                  [#bundle] => alumnos 
                  [#object] => Profile Object 
                   (
                    [pid] => 5 
                    [type] => alumnos 
                    [label] => Perfil Alumnos 
                    [uid] => 78 
                    [created] => 1358457398 
                    [changed] => 1358457398 
                    [entityType:protected] => profile2 
                    [entityInfo:protected] => Array 
                     (
                      [label] => Profile 
                      [plural label] => Profiles 
                      [description] => Profile2 user profiles. 
                      [entity class] => Profile 
                      [controller class] => EntityAPIController 
                      [base table] => profile 
                      [fieldable] => 1 
                      ... 
                      ... 
                      ... 

                    [field_nombrecompleto1] => Array 
                     (
                      [und] => Array 
                       (
                        [0] => Array 
                         (
                          [value] => Viviana 
                          [format] => 
                          [safe_value] => Viviana 
                         ) 

                       ) 

                     ) 

                    [field_cursobasico1] => Array 
                     (
                     ) 

                    [field_telefonos1] => Array 
                     (
                     ) 

                    [field_zona1] => Array 
                     (
                     ) 

                    [entity_view_prepared] => 1 
                   ) 

                  [#items] => Array 
                   (
                    [0] => Array 
                     (
                      [value] => Viviana 
                      [format] => 
                      [safe_value] => Viviana 
                     ) 

                   ) 
                   ... 
                   ... 
+0

錯誤已經足夠清楚了:變量'field_nombretopleto1'不存在。你確定你是print_r'ing正確的變量? –

+0

是的,這就是爲什麼我不明白... – Rosamunda

+0

可能重複[遞歸php函數,將嵌套數組轉換爲嵌套的html塊](http://stackoverflow.com/questions/1766995/recursive-php-function -that-turns-nested-array-into-nested-html-blocks) –

回答

2

看,你應該嘗試:

print $your_var['variables']['elements']['profile_alumnos']['view']['profile2'][5]['field_nombrecompleto1']['#object']->field_nombrecompleto1['und'][0]['value']; 

哪裏$your_var是您用於print_r變量。

+0

謝謝!它打印出我想要的輸出!爲什麼我應該使用與print_r一樣的var? – Rosamunda

+1

它不是你應該的,它是我關於數據結構的唯一信息。你可以引用你感興趣的路徑中的任何變量,然後從那裏引用。這完全取決於您打算如何處理這些數據,以及您需要訪問哪些部分。 –

+1

例如,您可以執行'$ field_nombrecompleto1 = $ your_var ['variables'] ['elements'] ['profile_alumnos'] ['view'] ['profile2'] [5] ['field_nombrecompleto1'] ['#object '] - > field_nombretopleto1;'然後'print $ field_nombretople1 ['und'] [0] ['value'];'(你的原始代碼)應該可以工作。 –