简体   繁体   English

为什么我不能在PHP中回显此多维数组的值?

[英]Why can I not echo the value of this multimensional array in PHP?

This is so incredibly basic that I am totally baffled as to why it doesn't work. 这是如此难以置信的基础,以至于我对为什么它不起作用感到困惑。 I have an array called $elements , and I want to just echo out one of the values. 我有一个名为$elements的数组,我只想回显其中一个值。

I use NetBeans as an IDE, and if I use that to examine the contents of the multidimensional array in question, it looks like this: 我将NetBeans用作IDE,如果使用它来检查所涉及的多维数组的内容,则它看起来像这样:

Netbeans中的数组视图

So far as I can tell, everything looks normal. 据我所知,一切看起来都很正常。 It is a multidimensional array, where the first level is numbered "0", and the second level has four named entries. 它是一个多维数组,其中第一个级别编号为“ 0”,第二个级别具有四个命名条目。

I just want to echo the value of "parameters", which is a string. 我只想回显“ parameters”的值,它是一个字符串。

However, this code outputs nothing: 但是,此代码不输出任何内容:

echo "This is the value of 'parameters': " .  $elements[0]['parameters'];

Have I got this most basic code wrong in some way? 我是否以某种方式弄错了这个最基本的代码?


This is what I get if I do var_dump($elements) : 这就是我执行var_dump($elements)

array(1) { [0]=> object(Element)#3 (4) { ["type":"Element":private]=> string(4) "Text" ["resource":"Element":private]=> string(1) "0" ["parameters":"Element":private]=> string(209) "IP1 111.111.111.111 IP2 222.222.222.222 IP3 333.333.333.333 IP4 444.444.444.444 IP5 555.555.555.555 IP6 666.666.666.666 IP7 777.777.777.777 IP8 888.888.888.888 IP9 999.999.999.999 IP10 111.111.111.112" ["parent":"Element":private]=> NULL } } array(1){[0] => object(Element)#3(4){[“” type“:” Element“:private] =>字符串(4)” Text“ [” resource“:” Element“:private ] =>字符串(1)“ 0” [“参数”:“元素”:专用] =>字符串(209)“ IP1 111.111.111.111 IP2 222.222.222.222 IP3 333.333.333.333 IP4 444.444.444.444 IP5 555.555.555.555 IP6 666.666 .666.666 IP7 777.777.777.777 IP8 888.888.888.888 IP9 999.999.999.999 IP10 111.111.111.112“ [” parent“:” Element“:private] => NULL}}

... and this is the output from print_r($elements) : ...这是print_r($elements)的输出:

Array ( [0] => Element Object ( [type:Element:private] => Text [resource:Element:private] => 0 [parameters:Element:private] => IP1 111.111.111.111 IP2 222.222.222.222 IP3 333.333.333.333 IP4 444.444.444.444 IP5 555.555.555.555 IP6 666.666.666.666 IP7 777.777.777.777 IP8 888.888.888.888 IP9 999.999.999.999 IP10 111.111.111.112 [parent:Element:private] => ) ) 

Your var dump is saying that element 0 is an object, so you will need to access it like so: 您的var转储说元素0是一个对象,因此您将需要像这样访问它:

echo $elements[0]->parameters;

The problem is that from your dump, the parameters element is marked as private, so you will not be able to access it. 问题是在转储中, parameters元素被标记为私有,因此您将无法访问它。

Solutions are: 解决方案是:

  • Change parameters to public parameters更改为公开
  • Write a getter ( getParameters() ) and use that method to get your parameters. 编写一个getter( getParameters() )并使用该方法获取您的参数。

Entry 0 at $elements is not just an array of attributes it's a class Element instance so in order to access its properties do something like: $elements条目0不仅仅是一个属性数组,它还是一个Element类实例,因此要访问其属性,请执行以下操作:

echo( $elements[ 0 ]->parameters );

Although the parameters field seems private so you'd better add an accessor method to the object like getParameters() which would be public and return the value of parameters. 尽管parameters字段似乎是私有的,所以您最好为getParameters()这样的对象添加一个访问器方法,该getParameters()将是公共的并返回parameter的值。

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

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