1

I am receiving this Json:

-{
"events" : -[
0-{
"eventName" : service-review-created,
"version" : 1,
"eventData" : -{
"id" : xxxxxxxx,
"language" : en,
"stars" : 5,
"title" : Friendly sales staff that are genuine,
"text" : Friendly sales staff that are genuine, helpful and most of all NOT pushy and annoying!,
"referenceId" : xxxxxxx,
"createdAt" : 2017-05-07T21:12:00Z,
"link" : https://api.trustpilot.com/v1/reviews/xxxxxxxxxxx,
"consumer" : -{
"id" : xxxxxxxxx,
"name" : Mrs xxxx xxxxx,
"link" : https://api.trustpilot.com/v1/consumers/590f8da00000ff000a9582aa
}
}
}
]
}

I am trying to extract the value "stars".

This is what I'm trying but nothing seems to be working:

$data = json_decode('{"events":[{"eventName":"service-review-created","version":"1","eventData":{"id":"xxxxxxx","language":"en","stars":5,"title":"Friendly sales staff that are genuine","text":"Friendly sales staff that are genuine, helpful and most of all NOT pushy and annoying!","referenceId":"xxxxxx","createdAt":"2017-05-07T21:12:00Z","link":"https://api.trustpilot.com/v1/reviews/590f8da02ecbfc09e8d3045d","consumer":{"id":"590f8da00000ff000a9582aa","name":"Mrs xxxxxxx","link":"https://api.trustpilot.com/v1/consumers/5xxxxxxxx"}}}]}');


        $stars = $data->events->eventData->stars;
        echo $stars;

Would be delighted if someone could tell me where I'm going wrong.

Lyrical.me
  • 215
  • 3
  • 15
  • You may find `var_dump` helpful to see what you're doing wrong. – Jakub Kania May 07 '17 at 22:29
  • Hi @Rizier123 - This is a bit of a noob question, granted. The question that you have marked as a duplicate contains a massively, massively complex (to a noob answer). I wouldn't consider that question and this question a duplicate at all. I just read through the other answer and it read like gibberish to me. This right here is a simple question and a simple solution. – Lyrical.me May 08 '17 at 14:39
  • Well you were asking how to access a particular value inside an object and the duplicate exactly explains you how to do it. – Rizier123 May 08 '17 at 14:42

2 Answers2

2

$data->events is an array, not object. You could do it like this $stars = $data->events[0]->eventData->stars

Max
  • 332
  • 2
  • 11
  • Thankyou - Frustratingly this was one of the many things that I tried and it didn't work. However now I see that it does. Must have had a typo or some other error when I tried it before. – Lyrical.me May 07 '17 at 22:29
  • This is the solution. Thank you! – Lyrical.me May 07 '17 at 22:44
0

IMO you'll have to cut into slices your data in order to manipulate them thoroughly.

$stars = ();
foreach($data as $key1 => $value1) {
  foreach($value1 as $key2 => $value2) {
    foreach($value2 as $key3 => $value3) {
      if ($key3 == 'eventData') {
        array_push($stars, $value3["stars"]);
      }
    }
  }
}

echo implode(", ", $stars);

I am new to manipulating JSON Data but this is the way i do it. Just tell me if this is not you are looking for. Thanks!

EDIT:

You can also do this if you'll only have one data on your events:

$stars = $data["events"][0]["eventData"]["stars"];

NOTE: I know this is a not the best method to extract a single but if you know one i'll be happy to listen

sshanzel
  • 379
  • 5
  • 18