PHP - 提取数组值(方括号与箭头表示法)

时间:2023-02-01 21:42:43

I have an object and when I user var_dump i get the following:

我有一个对象,当我用户var_dump时,我得到以下内容:

var_dump($object);

array (size=7)
  'id' => int 1969
  'alt' => string '' (length=0)
  'title' => string 'File Name' (length=9)
  'url' => string 'http://location/file.pdf' (length=24)

When I echo $object[url] it returns string 'http://location/file.pdf' (length=24), however when I echo $object->url I get null. (This sometimes works for extracting values from an array, I'm just not sure in which cases or what type of arrays)

当我回显$ object [url]时,它返回字符串'http://location/file.pdf'(长度= 24),但是当我回显$ object-> url时,我得到null。 (这有时用于从数组中提取值,我只是不确定在哪种情况下或什么类型的数组)

The problem is that I use a hosting provider that has a git feature, and when I push my changes, it checks the PHP code and encounters a syntax error (unexpected '[') which halts the push.

问题是我使用具有git功能的托管服务提供商,当我推送我的更改时,它会检查PHP代码并遇到语法错误(意外的'[')会停止推送。

My local server uses PHP 5.4.16 and the host uses 5.3.2

我的本地服务器使用PHP 5.4.16,主机使用5.3.2

On a broader note, can someone point me to a resource that explains the difference between these two methods of extracting values from arrays? Much appreciated!

更广泛地说,有人可以指向一个资源来解释这两种从数组中提取值的方法之间的区别吗?非常感激!

2 个解决方案

#1


8  

You don't have an object, you have an associative array. Note the very first word in your var_dump() output is:

你没有对象,你有一个关联数组。注意var_dump()输出中的第一个单词是:

array

Thus $object->url has no meaning and should in fact give you an error about trying to access property of a non-object.

因此$ object-> url没有意义,实际上应该给你一个关于尝试访问非对象的属性的错误。

I could not imagine any PHP-related syntax checking process which would throw an error on something like $foo['bar']. Whether it is an object or array would typically not be able to be determined by a simple syntax check. You may have other typos somewhere preceding that are causing this though.

我无法想象任何与PHP相关的语法检查过程会在$ foo ['bar']之类的东西上引发错误。无论是对象还是数组,通常都无法通过简单的语法检查来确定。你可能在之前的某个地方有其他拼写错误。

#2


5  

You can't access arrays this way : -> that only applies to objects like the following:

您无法以这种方式访问​​数组: - >仅适用于以下对象:

$x = (object) array('a'=>'A', 'b'=>'B', 'C'); gives you:

stdClass Object
(
    [a] => A
    [b] => B
    [0] => C
)

To access these values you have to do it the way with -> like so:

要访问这些值,您必须使用 - >像这样:

$valA = $x->a;

#1


8  

You don't have an object, you have an associative array. Note the very first word in your var_dump() output is:

你没有对象,你有一个关联数组。注意var_dump()输出中的第一个单词是:

array

Thus $object->url has no meaning and should in fact give you an error about trying to access property of a non-object.

因此$ object-> url没有意义,实际上应该给你一个关于尝试访问非对象的属性的错误。

I could not imagine any PHP-related syntax checking process which would throw an error on something like $foo['bar']. Whether it is an object or array would typically not be able to be determined by a simple syntax check. You may have other typos somewhere preceding that are causing this though.

我无法想象任何与PHP相关的语法检查过程会在$ foo ['bar']之类的东西上引发错误。无论是对象还是数组,通常都无法通过简单的语法检查来确定。你可能在之前的某个地方有其他拼写错误。

#2


5  

You can't access arrays this way : -> that only applies to objects like the following:

您无法以这种方式访问​​数组: - >仅适用于以下对象:

$x = (object) array('a'=>'A', 'b'=>'B', 'C'); gives you:

stdClass Object
(
    [a] => A
    [b] => B
    [0] => C
)

To access these values you have to do it the way with -> like so:

要访问这些值,您必须使用 - >像这样:

$valA = $x->a;