2013-01-17 162 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'];'(你的原始代码)应该可以工作。 –