32

I've been using PHP quite a while now, but never been an advanced programmer. I feel like this is dumb question but never understood why some array values can be retrieved using different methods:

This:

$array->value

rather than normal:

$array['value']

The standard $array['value'] always works, but the one using the -> method doesn't at times. Why is that?

Here's an example. I am using Zend Framework 2 and I can grab a session value using the -> method:

$this->session->some_value

However, I can't if I do a new, normal array:

$array = array('some_value' => 'myvalue');
$array['some_value']; // works!!
$array->some_value;  // does not work :(

In Zend Framework 1 most arrays would work fine this way, and in ZF2 more and more , I run into issues where I need to change the way I get that value. Does this make sense? I sure appreciate any help. Thanks, Greg

1
  • Thanks to all for the great answers. You are all right about what I was looking for. Voitcus (below) had the key to help me understand better. Thank you,everyone, for the help! Commented May 18, 2013 at 22:42

5 Answers 5

30

As stated before in other answers, using -> means you are accessing an object, not an array.

However, it is sometimes possible that an object would be treated as an array. It is when it is implementing ArrayAccess interface. The coder can do such that eg. calling $object->field would be equivalent to $object['field'], but he/she must not.

Moreover, it is possible to treat an array as an object (refer to the manual), however in this case it is not an array but an object and is the same way as above.

Sign up to request clarification or add additional context in comments.

1 Comment

This is what I thought and is exactly what I was looking for, just didn't know the exact reasons. PHP's ArrayObject, I think, is what caused my confusion when using ZF 1 or 2, as the objects mostly looked and behaved like array's. Now I get it! Yay! After reading your comment and linked doc, I found this page which helped my understanding as well. Thanks!! http://codingexplained.com/coding/php/zend-framework/using-sessions-in-zend-framework-2
2

The variables that allow you get properties with -> are actually objects, not arrays. They do allow the ['some_key'] syntax, but that doesn't mean they are arrays. They are not.

You can reading more about objects on this page of the PHP manual.

Comments

2

To make it happened just using a stdclass cast

$optionsType = array(
    (object)  array("text" => "Virtual Account", "value" => "TOPUPVA"),

and you could access it by

foreach($optionsType as $key=>$value){
    var_dump($value->text);

As stated by Voitcus answer

using -> means you are accessing an object, not an array.

so if you insists to use dash arror (->) than you can casting your array to object (which is make your program consume more memory)

Comments

1

Those aren't arrays, they're objects.

Comments

1

That is because it is not an array it is an objects variable.

For example;

class MyObject{

   var $myVariable = 'test';

}

$MyObject = new MyObject();
echo $MyObject->myVariable; // Would return 'test'

Comments

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.