0

I have an array, containing an object. I need the value of a property of the first object but somehow I get an empty value.

My array $params (from print_r) looks like this:

Array
(
[newOrderStatus] => OrderState Object
    (
        [name] => Canceled
        [template] => order_canceled
        [send_email] => 1
        ...

Cut off here, there are two more objects in this array.

Now if I do: echo $params[0]->name I get an empty result.

Also tried print_r($params[0], true);, empty result.

Also tried, empty result:

$status = $params[0];
echo $status->name;

What am I doing wrong here?

Thanks in advance

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36

4 Answers4

2

Well, as you said your array looks like this :

Array
(
  [newOrderStatus] => OrderState Object
  (
    [name] => Canceled
    [template] => order_canceled
    [send_email] => 1
    ...

So there is no $param[0], you should do $param['newOrderStatus'] and then get what you want : $param['newOrderStatus']->name

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
1

Your array $params has a key called newOrderStatus which has the object as a value you are looking for.

Looking at your example, there is value for index 0.

To get the value of the name property, you could use:

$params['newOrderStatus']->name

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
1

You need to access object as following

$params['newOrderStatus'];

In above object you will have all child objects so you can access them by following

$params['newOrderStatus']->name;
$params['newOrderStatus']->template;
Ayaz Ali Shah
  • 3,453
  • 9
  • 36
  • 68
0

You can type cast it to an array like this:

$array =  (array) $yourObject;
lifeisbeautiful
  • 817
  • 1
  • 8
  • 18