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

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

问题描述

我在Google上安装了Devel,Drupal for Firebug,但是找不到它。

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.

我将其放在代码括号中,但Devel告诉我文件名(我想要粘贴到.tpl.php文件)在这里:

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

那么,如何使用PHP输出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.

对于一个简单的变量值,这很简单,您只需使用变量名称并将其访问为一个变量用$ code $ $ / code>符号前缀:

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);

这个在这里记录

但是,这只适用于简单的数据类型,如 string 整数。还有复合数据类型,即 array 对象。它们可以包含更多的数据类型,无论是简单还是复合。您可以在PHP手册中了解如何访问数组的值以及您可以从对象访问它们。我想你已经知道了这一点,所以只是为了让它在这里链接。

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.

某些组合示例:

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

这是基于上面提供的数据。 $ variable 是您开始的地方, - > 用于访问需要命名的对象成员(如一个变量的名称):处理程序。正如您在调试输出中看到的, handler 是一个对象,您需要再次使用 - > 访问视图成员。

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.

现在视图是不同的,因为它是一个数组。您可以使用 [] 并将密钥放在那里来访问数组的值。我的例子中的关键是一个数字, 0 。而且由于该数组项的值是一个对象,在下一步中,您需要再次使用 - >

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.

然而,当找到东西时,使用 var_dump ,您可以逐步扩展表达式,直到找到该元素。如果您发生错误,您将立即看到。有时,可以在 var_dump 语句之后放置一个 die(); ,所以在它包含之前不要结束响应许多其他数据将隐藏您的信息。 devel插件提供额外的调试例程来转储突出的值。

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天全站免登陆