Ramesh_Konda
Ramesh_Konda

Reputation: 111

how to convert string(which in array form) to array/object

I am doing payu automation refunds. In that after sending the request am
getting response as

$res =  Array (
    [status] => 0
    [msg] => Refund FAILURE - Invalid amount
    [error_code] => 105
    [mihpayid] => 569611073
   )

But if check gettype($res) its coming as string... Here am not able to get key and value pairs using

$res['status'] or $res['msg']

Its giving

A PHP Error was encountered
     Severity: Warning
     Message:  Illegal string offset 'status'
     Filename: pgrefunds/pgrefunds.php
     Line Number: 296

suggest me how can get the kay value pairs...

Upvotes: 0

Views: 85

Answers (2)

nerdlyist
nerdlyist

Reputation: 2847

Most likely you are getting a serialized response. You should unserialize the string.

$resArr = unserialize($res);
$resArr['status'];

EDIT

As other answer show the other option is to you json_decode()

$resArr = json_decode($res);
$resArr['status'];

Upvotes: 0

Miguel G. Flores
Miguel G. Flores

Reputation: 822

In the PayU documentation (https://developer.payubiz.in/documentation/Request-and-Response-format/110) you can read this line:

Web Service API responds back in PHP serialized string by default.

So you must unserialize the content before using it. Depends on the configuration the content can come as JSON or in "array" form (serialized). Depends on the case you must use json_decode or unserialize.

Upvotes: 1

Related Questions