-5

I have a JSON data which I am trying to parse, but my code is not working. I get the error

unexpected '->' (T_OBJECT_OPERATOR).

The array structure looks like this:

Array
(
    [mautic.lead_post_save_update] => Array
        (
            [0] => Array
                (
                    [lead] => Array
                        (
                            [isPublished] => 1
                            [dateAdded] => 2016-09-15T08:08:20+00:00
                            [createdBy] => 1
                            [createdByUser] => Deepak Tiwari
                            [dateModified] => 2016-12-20T14:20:36+00:00
                            [modifiedBy] => 1
                            [modifiedByUser] => Deepak Tiwari
                            [id] => 149269
                            [points] => 100
                            [color] => 
                            [fields] => Array
                                (
                                    [core] => Array
                                        (
                                            [compulsation] => Array
                                                (
                                                    [id] => 39
                                                    [label] => Job 
                                                    [alias] => compulsation
                                                    [type] => text
                                                    [group] => core
                                                    [field_order] => 2
                                                    [object] => lead
                                                    [value] => 0

and so on. I need to access the value of "job compultion". The code I am using is:

$json = file_get_contents('php://input');

$data=json_decode($json,TRUE);
$job=data->mautic.lead_post_save_update->0->lead->fields->core->compulsation->value;

I am not getting any value in $job.

elixenide
  • 44,308
  • 16
  • 74
  • 100
Deepak Tiwari
  • 324
  • 1
  • 3
  • 12

2 Answers2

1

You're using object property syntax (->) to access array elements, which won't work. Your code should use array syntax ([...]), like this:

$json = file_get_contents('php://input');

$data=json_decode($json,TRUE);
$job=data['mautic.lead_post_save_update'][0]['lead']['fields']['core']['compulsation']['value'];

That's what the error message is trying to tell you. A decent IDE, like PHPStorm (no affiliation), would help you catch errors like this.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • Thanks a lot!! , it worked.I am wondering why -> is not working , where I have other code in which I am using -> to access – Deepak Tiwari Dec 20 '16 at 15:16
  • @DeepakTiwari `->` is the correct syntax if you are accessing a property of an *object*. If you have an *array*, which is what your `print_r()` output above shows that you have, then you must use brackets (`[` and `]`). – elixenide Dec 20 '16 at 15:19
  • thank you. I am new to php. Thanks for saving my life. I was trying this from last 15-20 hours – Deepak Tiwari Dec 20 '16 at 15:23
0

Your json_decode() is not creating a object. Its an array and you can get your variables by using [] instead of the ->

MHopstad
  • 333
  • 2
  • 16