能够在 print_r() 的输出中看到一个变量,但不确定如何在代码中访问它 [英] Able to see a variable in print_r()'s output, but not sure how to access it in code

查看:14
本文介绍了能够在 print_r() 的输出中看到一个变量,但不确定如何在代码中访问它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用谷歌搜索,为 Firebug 安装了 Devel、Drupal,但找不到.

我找到了我想要的,我知道在哪里;我只是不知道如何获得它.

我将把它放在代码括号中,但 Devel 告诉我文件名(我想放在 .tpl.php 文件中)在这里:

<前>field_image (Object) stdClass处理程序(对象)views_handler_field_field视图(对象)视图结果(数组,2 个元素)0(对象)标准类_field_data(数组,1 个元素)nid(数组,2 个元素)实体(对象)stdClassfield_image(数组,1 个元素)und(数组,1 个元素)0(数组,11 个元素)文件名(字符串,23 个字符) FILENAME.jpg

那么,如何使用 PHP 输出 FILENAME.jpg?

other;?>

解决方案

每当您需要从变量中读取值时,您都需要知道需要制定哪个表达式来访问该值.

对于一个简单的变量值,这很简单,您只需获取变量名并通过在它前面加上 $ 符号作为变量来访问它:

var_dump($variable);

记录在此处.

然而,这仅适用于简单的数据类型,例如 stringinteger.还有复合数据类型,即arrayobject.它们可以包含更多数据类型,无论是简单的还是复合的.您可以在 PHP 手册中了解如何访问数组的值以及如何您可以从对象访问它们.我想你已经知道了一点,所以只是把它链接到这里.

当您了解了这一点后,您就可以将其结合起来.例如.如果对象中有一个数组并且其中有一个您想要获取的字符串,您需要将 $ 符号和变量名称与所需的访问器、属性名称和数组键组合起来.然后你得到你的价值.您发布的数据显示您有一个包含其他一些对象和数组的对象,最后您找到了变量名称.

一些组合示例:

var_dump($variable->handler->view[0]->_field_data);

这是基于您在上面提供的数据.$variable 是你开始的地方,-> 用于访问需要命名的对象成员(就像变量的名称):handler.正如您在调试输出中看到的那样,handler 是一个对象,您需要再次使用 -> 来访问 view 成员

现在 view 不同,因为它是一个数组.您可以通过使用 [] 并将键放在那里来访问数组的值.我的例子中的关键是一个数字,0.由于该数组条目的值又是一个对象,因此在下一步中您需要再次使用 ->.

您可以继续这个游戏,直到到达您感兴趣的元素.您已有的调试输出帮助您编写返回值的表达式.可能是:

$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['文档名称']

但我无法在我的系统上完全验证这一点.

然而,在找出问题时,使用 var_dump 很有帮助,因为您可以逐步扩展表达式,直到找到元素.如果您犯了错误,您将立即看到.有时,在 var_dump 语句之后放置一个 die(); 会有所帮助,这样在响应包含许多其他会隐藏信息的数据之前不要结束响应.devel 插件提供了额外的调试例程来转储突出的值.

I googled, installed Devel, Drupal for Firebug, but I can't find it.

I found what I want, I know where it is; I just don't know how to get it.

I'll put this in code brackets, but Devel tells me the file name (which I want to stick into the .tpl.php file) is here:

field_image (Object) stdClass 
  handler (Object) views_handler_field_field 
    view (Object) view 
      result (Array, 2 elements) 
        0 (Object) stdClass 
          _field_data (Array, 1 element) 
            nid (Array, 2 elements) 
              entity (Object) stdClass
                field_image (Array, 1 element) 
                  und (Array, 1 element)
                    0 (Array, 11 elements)                                      
                      filename (String, 23 characters ) FILENAME.jpg

So, how do I get that FILENAME.jpg to be output using PHP?

<?php print $something->other; ?>

解决方案

Whenever you need to read a value out of a variable, you need to know which expression you need to formulate to access that value.

For a simple variable value this is simple, you just take the variable name and access it as a variable by prefixing it with the $ sign:

var_dump($variable);

This is documented here.

However this does only work for simple datatypes like string or integer. There are as well compound datatypes, namely array and object. They can contain further datatypes, be it simple or compound. You can learn in the PHP manual how to access the values of an array and how you can access them from an object. I think you already know of that a bit, so just for having it linked here.

When you have learned about that, you can then combine this. E.g. if there is an array within an object and therein is a string you would like to get, you need to combine the $ sign and the variable name with the needed accessors, property names and array keys. Then you get your value. The data you have posted shows that you have an object that has some other objects and arrays and in the end you find the variable name.

Some combination example:

var_dump($variable->handler->view[0]->_field_data);

This is based on the data you've provided above. $variable is where you start, -> is used to access object members which need to be named then (like a name for a variable) : handler. As you've seen in your debug output that handler is an object, you need to use again the -> to access the view member of it.

Now view is different because it's an array. You access values of an array by using [] and putting the key in there. The key in my example is a number, 0. And as the value of that array entry is an object again, in the next step you need to use -> again.

You can continue this game until you reach the element that you're interested in. The debug output you already have helps you to write the expression that returns the value. Possibly it is:

$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']

But I can not validate that here on my system in full.

However when finding things out, it's helpful to make use of var_dump as you could step by step extend the expression until you find the element. If you make an error you will immediately see. Sometimes it helps to place a die(); after the var_dump statement so not to end the response before it contains to much other data that will hide the information from you. The devel plugin offers additional debug routines to dump values prominent.

这篇关于能够在 print_r() 的输出中看到一个变量,但不确定如何在代码中访问它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆