0

I have an array $pname in view which I accessed from controller, when I write print_r($pname); output is:

Admin\Model\Provinces Object ( [id] => 4 [p_name] => Khyber Pakhtunkhwa [inputFilter:protected] => )

I need to show Khyber Pakhtunkhwa in view:

I tried echo $pname['p_name']; but it show me an error:

Fatal error: Uncaught Error: Cannot use object of type Admin\Model\Provinces as array in C:...

So how could I do this, i.e., access specific data from an array.

1 Answer 1

1

In your case $pname is NOT an array, it is an object of type Admin\Model\Provinces (as you can clearly see in the output of your print call).

To access propeties of simple php objects you can sometimes use array notation as you do (read more on this here on php.net), but in this case it isn't allowed on this type of object hence the error message.

You can access the object properties in your object probably by using the arrow notation (in case a magic getter is implemented) or using the getters provided in the class Admin\Model\Provinces.

This is basic PHP stuff I suggest you get more familiar with PHP before you start working in a complex framework like Zend-Framework 2.

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

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.