-1

I am getting data like the following:

$getData = $series->getData($post_id);
Array
(
    [0] => stdClass Object
        (
            [id] => 2
            [month] => December 29 
            [image] => https://www.example.com/uploads/post/Post-20190920111041434.jpg
            [added_date] => 2019-11-02
        )

)

If I try to access $getData->month I don't get any data and if I try to access data like $getData[0]->month I get data but I also get list of errors in my log file which says Undefined offset: 0. What is wrong here?

1 Answers1

-1

Edit:

I just learned that it can be achieved by a simpler way.

echo $getData[0]->month;

you can also make this work by doing following but it's not an efficient way.

$getData = $series->getData($post_id);
$getData = json_decode(json_encode($getData), True);

And get the value by using loop if you have multiple objects or you can simply do

echo $getData[0]['month'];
Umer Abbas
  • 1,866
  • 3
  • 13
  • 19
  • whats wrong with the exiting object $getData, why json_decode(json_encode? – Lawrence Cherone Oct 01 '19 at 15:41
  • you have an std class object array, i converted it into a simple array which is much easier to work with. I converted the result std class object array into json and then converted it back into php array with `json_decode($getData, True)` the second parameter optional but in this case it's very important to convert the json into simple php array. – Umer Abbas Oct 01 '19 at 15:46
  • @LawrenceCherone if there is a better approach you can point me in that direction, I would really appreciate that. – Umer Abbas Oct 01 '19 at 15:53
  • echo $getData[0]->month; – Lawrence Cherone Oct 01 '19 at 15:54
  • @LawrenceCherone Thank you. I will edit my answer. – Umer Abbas Oct 01 '19 at 15:56