1

Object having a value var $error_code="100"; I can set this value inside a function as

$this->error_code=$simpleXml->error_code;  // eg:214

I can create its object and print value using json_encode as

 echo $obj->error_code; //print value 214
 $code=$obj->error_code;
 $set=array("error_code" => $code,"message" =>"topup failed");
 echo json_encode($set);

I got response as

214 {"error_code":{"0":"214"},"message":"topup failed"}

but I expect output as

{"error_code":"214","message":"topup failed"}

what is the actual problem for getting

{"0":"214"} 

on output even if $code print value 214 ?

7
  • what is dump of $obj ? Commented Jun 29, 2015 at 14:11
  • 1
    there is no way json_encode() would output invalid json. it either outputs VALID json, or it outputs a boolean false to signify failure... Commented Jun 29, 2015 at 14:12
  • Object ( [balance] => 5 [username] => werwerw [password] => ewrewrwer [oprid] => -1 [destination] => [transactionid] => [error_txt] => SimpleXMLElement Object ( [0] => Fraud suspicion ) [error_code] => SimpleXMLElement Object ( [0] => 221 ) ) Commented Jun 29, 2015 at 14:16
  • I think problem with assigning value by simplexml obj.is it ok? Commented Jun 29, 2015 at 14:17
  • Get rid of your first echo statement, as it is producing output. Commented Jun 29, 2015 at 14:40

2 Answers 2

1

Change this line,

$code=$obj->error_code;

to

$code=$obj->error_code->0;

Also remove

echo $obj->error_code;

to get

{"error_code":"214","message":"topup failed"}
Sign up to request clarification or add additional context in comments.

1 Comment

It printed as {"error_code":{},"message":"topup failed"}
0

Instead of :

echo $obj->error_code; //print value 214
$code=$obj->error_code;
$set=array("error_code" => $code,"message" =>"topup failed");
echo json_encode($set);

Just use:

$code = $obj->error_code;
echo json_encode(array("error_code" => $code,"message" =>"topup failed"));

Remove the other lines, they are unnecessary.

Edit: Add the [0] after $obj->error_code

3 Comments

get the same output {"error_code":{"0":"221"},"message":"topup failed"}.
I set value from a simplexmlobject
then I got {"error_code":{},"message":"topup failed"}

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.