-1

I have this JSON in socket client

{ 
  "DATA":
     [
      {
       "message": my_message,
       "name": my_name,
       "id": my_id,
      }
     ]
}

And i have to decode this in socket server to respond with another JSON,like below:

$data = json_decode($received_text); //json decode 
$user_message = $data['DATA']->message; //message text
$user_name = $data['DATA']->name; //sender name 

But can't access it, What is wrong in it?

Aqil
  • 104
  • 10

1 Answers1

0

You need to change your code like below

$received_text =  '{"DATA":[{"message":"my_message","name":"my_name","id":"my_id"}]}';

$data = json_decode($received_text); //json decode 
$data = $data->DATA[0];

$user_message = $data->message; //message text
$user_name = $data->name; //sender name 

echo $user_message . "<br>". $user_name;
Rohit.007
  • 3,414
  • 2
  • 21
  • 33
  • Please don't beg for up votes. If you want up votes, you can get those by providing higher quality answers. Here it would be useful to tell _what_ you changed, as well as why that fixed the problem. Keep in mind that Q&A's on Stack Overflow are to help the community, not just the OP. (It also strikes me how you were able to answer this question more then 30 minutes after it got closed as a duplicate.) – Ivar Jul 28 '18 at 23:26