PHP-提取数组值(方括号与箭头表示法) [英] PHP - extracting array values ( square brackets vs arrow notation )

查看:238
本文介绍了PHP-提取数组值(方括号与箭头表示法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,当我使用 var_dump 时,我得到以下信息:

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

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)

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

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)

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

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.

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

My local server uses PHP 5.4.16 and the host uses 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!

推荐答案

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

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

array

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

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

我无法想象任何与PHP相关的语法检查过程会在诸如 $ foo ['bar'] 之类的地方抛出错误。通常是无法通过简单的语法检查来确定是对象还是数组。但是,您可能在前面的其他地方输入了其他错字。

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-提取数组值(方括号与箭头表示法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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