16

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)

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)

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.

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!

1
  • 4
    $var->foo is an object reference. $var[foo] is an array reference. While this sort of thign could be used interchangeably in some languages like javascript, in PHP an array and an object are two entirely different things. Commented Oct 3, 2014 at 14:52

2 Answers 2

13

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;
Sign up to request clarification or add additional context in comments.

Comments

11

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

array

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

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.

6 Comments

To wit, -> is for accessing object with an object that implements ArrayAccess.
Yes I am surprised by the error as well, but it only occurs when the bracket notation is present.
@PeteSorensen Maybe a question for your hosting provider (or see if you can have them turn of syntax chaecking, as it would seem odd to run this as part of a push process.
After using a syntax checker, it seems to be a problem with 5.3 and not 5.4. Still confused how to extract the single array value in 5.3
@PeteSorensen In your case to get the url, you would need to do $object['url']. Of course $object is a really poor name for the variable in this case.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.