0
 $postdata= "stdClass Object
    (
    [created] => 1326853478
    [livemode] => 
    [id] => evt_00000000000000
    [type] => invoice.payment_succeeded    
...........................
        ))";

$event = json_decode($postdata);
echo "<pre>";
print_r($event);

Can't std class object to array in php?I am using json_decode but its not working just get empty array.I am giving my result data std class object assign in variable its anything wrong that's why its showing not working?

6
  • That can't be that actual data you're trying to JSON decode, right? Commented Dec 23, 2015 at 11:31
  • As a side note, with json_decode, setting the second arg to true will return you an array (though currently it looks like you're trying to decode the output of a var_dump O_o Commented Dec 23, 2015 at 11:33
  • assign that std class object is wrong?$event = json_decode($postdata,true); var_dump($event); exit; Get null value Commented Dec 23, 2015 at 11:33
  • $postdata is not an object in this case, it is a string. This string is also not valid JSON. If it was an object you could simply do $event = (array) $postData. Is there a reason you have it this way? Commented Dec 23, 2015 at 11:36
  • 2
    Everyone is confused. Is that your actual code or is the stdClass output from a print_r() or something Commented Dec 23, 2015 at 11:36

3 Answers 3

2

you can use :

$array = json_decode(json_encode($data), true);
print_r($array);
Sign up to request clarification or add additional context in comments.

Comments

2

The easiest way is to cast it:

$array = (array) $stdClass;

Comments

0

try this

$event = json_decode($data, true);
print_r($event);

Comments

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.