Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!