2
subscription
Object {id: "3", type: "all", name: "Order Payment Status Fully Paid", category_id: "-1", admin_contact_selector: "admin-1"…}
admin_contact: Object
admin_contact_selector: "admin-1"
category: Object
display: "flight: [All]"
id: "-1"
product_type: "flight"
__proto__: Object
category_id: "-1"
id: "3"
name: "Order Payment Status Fully Paid"
type: "all"
__proto__: Object

I have subscription object with the id value. I want to display the value of display in the category object.

print "<pre>";
var_dump($subscription['category']['dispaly']);
print "</pre>";
?>

my problem return value is null, real value is flight: [All]

Thanks a lot

3
  • 1
    you have a typo "dispaly" should be "display" Commented Feb 18, 2014 at 10:14
  • you are trying to access an object.... So you need to access it like this echo $subscription->category->display; If you want to access it as an array then you need to get the db results using ->result_array(); or ->row_array(); instead of using ->row() <-- assuming ofc that you are using DB results :) Commented Feb 18, 2014 at 10:23
  • Yes bro, I using wrong spelling . Commented Feb 18, 2014 at 10:31

2 Answers 2

1

It should be like ..

echo $subscription->category->display;

That is because you are accessing objects and you need to make use of the -> operator. You got a null because of two reasons

  • First, you were accessing the objects as arrays.
  • Secondly, you had a typo. If eventhough it was right, that would still display null as your first condition fails.
Sign up to request clarification or add additional context in comments.

Comments

0

Make use of (array) variable-transfor feature provided by PHP. For example if $user is your object, then $userArray = (array) $user, $userArray will contain the array format of user object. Each property of user obect can now be accessed simply by $userArray['PROPERTYNAME']. Make sure not to use this when your object contains methods.

1 Comment

this is ok, but it does not apply recursively which can be a problem when working with nested objects.

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.