简体   繁体   English

如何在多维数组中打印出单个字段?

[英]How can I print out a single field in my multidimensional array?

So I do a print for my object: print_r ($objMailer); 所以我为我的对象做一个打印:print_r($ objMailer);

And I get the following below: 我得到以下内容:

mymailer Object
(
[_strRecipient:mymailer:private] => 
[_strBcc:mymailer:private] => 
[_strSubject:mymailer:private] => 
[_strEmail:mymailer:private] => 
[_arrData:mymailer:private] => Array
(
    [full_name] => brian
    [invitee_name] => test
    [email] => test@testing.com
    [captcha] => kqd2q9
)

[_arrAttachments:mymailer:private] => 
[_blnCaptcha:mymailer:private] => 1
[_arrErrors:mymailer:private] => Array
(
)

)

I need to echo/print out just the 'full_name' field? 我只需要回显/打印“ full_name”字段? How can I go about doing this? 我该怎么做呢?

You can not trivially. 你不能平凡。 As the print_r output shows that is within a private member. print_r输出所示,该消息位于私有成员内。

You can either provide it from within your (?) mymailer object: 您可以从(?) mymailer对象中提供它:

return $this->_arrData['full_name'];

or by using Reflection to make it accessible from the outside: 或使用反射使它可以从外部访问:

$refObj  = new ReflectionObject($objMailer);
$refProp = $refObj->getProperty('_arrData');
$array   = $refProp->getValue($objMailer);

echo $array['full_name'];

如果要在mymailer类的方法中回显值,则可以使用:

echo $this->_arrData['full_name'];

由于是私人的,因此您需要使用吸气剂

The object you are referencing has an _arrData member variable which has private scope resolution, which means you cannot access it from outside the class. 您所引用的对象具有_arrData成员变量,该成员变量具有私有范围的解析度,这意味着您无法从类外部访问它。 Chances are there is a public accessor which will allow you get the information you are after, but no way to tell unless you introspect the object itself. 可能会有一个公共访问器,该访问器将使您获得所需的信息,但是除非您对对象本身进行内省,否则无法告诉。

I would suggest doing something like: 我建议做这样的事情:

foreach (get_class_methods($mymailer) as $method) { echo 'M: ' . $method . '<br>'; } exit;

Then you can see the methods available to you, chances are there is a getData() method, with which you can do this: 然后,您可以看到可用的方法,可能有一个getData()方法,您可以使用该方法执行此操作:

$mailerData = $mymailer->getData();

var_dump($mailerData['full_name']);

There may even be a method to get the full name, something like this: 甚至可能有一种获取全名的方法,如下所示:

var_dump($mymailer->getFullname());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM