Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: add variables into json_decode

I want to add variables into a string in php. I googled and found the method of : "{$a}". However this doesn't work for me. I echoed it and it has literally echoed "{$a}" instead of the actual value of the variable. Here is my code:

$body= json_decode(
                            '{
                                "sender_batch_header":
                                {
                                  "email_subject": "SDK payouts test txn"
                                },
                                "items": [
                                {
                                  "recipient_type": "EMAIL",
                                  "receiver": "{$email}",
                                  "note": "Your 1$ payout",
                                  "sender_item_id": "Test_txn_12",
                                  "amount":
                                  {
                                    "currency": "CHF",
                                    "value": "{$actualAmount}" 
                                  }
                                }]
                              }',             
                            true);

And here is the echo of $body:

{ "sender_batch_header": { "email_subject": "SDK payouts test txn" }, "items": [ { "recipient_type": "EMAIL", "receiver": "{$email}", "note": "Your 1$ payout", "sender_item_id": "Test_txn_12", "amount": { "currency": "CHF", "value": "{$actualAmount}" } }] }

The variables I am talking about are $email and $actualAmount. I debugged it and they both have a value it just does not get added to the string. Can anyone help?

like image 863
nooby98 Avatar asked Jun 10 '26 22:06

nooby98


1 Answers

A string literal can be different ways:

  • single-quoted
  • double quoted

You can use braces in double-quoted strings like: "Hello {$foo}" unfortunately your string variable is single-quoted. So we can use the concatenation operator ('.'), which returns the concatenation of its right and left arguments.

So let's try with concatenation operator;

 $body= json_decode(
                                '{
                                    "sender_batch_header":
                                    {
                                      "email_subject": "SDK payouts test txn"
                                    },
                                    "items": [
                                    {
                                      "recipient_type": "EMAIL",
                                      "receiver": "'.$email.'",
                                      "note": "Your 1$ payout",
                                      "sender_item_id": "Test_txn_12",
                                      "amount":
                                      {
                                        "currency": "CHF",
                                        "value": "'.$actualAmount.'" 
                                      }
                                    }]
                                  }',             
                                true);
like image 73
Orhan Erday Avatar answered Jun 13 '26 10:06

Orhan Erday



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!