1

I am new to PHP, I am trying to learn it. Actually, I am not getting what is this format and how to get the result from it. below is my code after var_dump:

$data = some array();
var_dump($data);
/*
 object(stdClass)#58 (3) {
    ["jsonrpc"]=> string(3) "2.0"
    ["id"]=> int(1)
    ["result"]=> string(42) "Success"
} 
*/

Now I want the 'result' parameter only as a string. I am trying to do this way:

$reult = $data['result'];

but it is giving Illegal string offset 'result' error.

mickmackusa
  • 33,121
  • 11
  • 58
  • 86
  • try `$data->result`. If it works, it's not an array but a [`StdClass`](https://stackoverflow.com/questions/931407/what-is-stdclass-in-php). If it doesn't work, something else is wrong in your code. – yqlim Apr 18 '18 at 03:41
  • Possible duplicate of [PHP basic: How to access a member of an attribute of an object](https://stackoverflow.com/questions/8451248/php-basic-how-to-access-a-member-of-an-attribute-of-an-object) – mickmackusa Apr 18 '18 at 04:11

2 Answers2

1
echo $data->result;

You can use this to echo the object value

Or you can cast it as array

var_dump( (array) $data);
$data=(array) $data;
echo $data[result];
Wils
  • 1,149
  • 6
  • 22
-1

Your data is a PHP stdClass. In my opinion you should convert it to array. After that you can easily use it.