0

I am trying to build some JSON on the in PHP. I am brand new to PHP and know very little about it. Currently, I have the following:

$json = '{"content":{"person":{"name":"$name", "email":"$email"}, "title":"$subject", "description": { "body": "$details" }}}';

$name, $email, $subject, and $details are all variables that have been defined previously. If I print out $json using the code above, I get the following:

{"content":{"person":{"name":"$name", "email":"$email"}, "title":"$subject", "description": { "body": "$details" }}}'

In other words, my variables didn't replace the placeholders in the string. How do I build some JSON using variables as key values?

Thank you!

2
  • 5
    Build an array containing the data and structure you desire. Then use json_encode() to convert it to JSON. Commented Apr 2, 2014 at 0:50
  • 1
    And for future: PHP Strings Commented Apr 2, 2014 at 0:52

4 Answers 4

1

In PHP, double quotes and single quotes do different things; you can only use variables inline within strings if you use double quotes:

$test = 'world';
echo 'Hello $test!'; // Prints: Hello $test!
echo "Hello $test!"; // Prints: Hello world!

If you use double quotes to surround your json string, you'd need to escape all the double quotes that you have inside it:

$json = "{\"content\":{\"person\":{\"name\":\"$name\", \"email\":\"$email\"}, \"title\":\"$subject\", \"description\": { \"body\": \"$details\" }}}";

Alternative Method

NB: Are you familiar with PHP arrays? PHP has the function json_encode that converts arrays into JSON strings - doing that might make your code easier (especially if your json string is going to get larger / more complex at any point)

$json = json_encode(array
(
    "content" => array
    (
        "person"      => array
        (
            "name"  => $name,
            "email" => $email
        ),
        "title"       => $subject,
        "description" => array
        (
            "body" => $details
        )
    )
));

Either of these solutions should give $json the value you expect

Hope this helps :) x

Sign up to request clarification or add additional context in comments.

Comments

1

As mentioned in the comments, the best way would be to build up your data into the structure that you want and use json_encode to fix it.

The reason that you particular string is not replacing the variables is because it is enclosed by ' instead of "

$json = "{\"content\":{\"person\":{\"name\":\"$name\", \"email\":\"$email\"}, \"title\":\"$subject\", \"description\": { \"body\": \"$details\" }}}'\";

http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing

Comments

1
$content = json_encode(array(
'content' => array(
     'person' => array(
         'name' => $name,
         'email' => $email,
         'title' => $subject,
         'description' => array(
             'body' => $details
             )
         )
   )
);
echo $content;

Comments

0

The documentation is always very helpful, check out the ones for strings, here it will explain how you can and cannot use variables in single quoted or double quoted strings.

Documentation on Strings

An easier solution is to use an array and json_encode it which will output what you have in your question:

<?php
$array = array(
    'content' => array(
        'person' => array(
            'name'  => $name,
            'email' => $email,
        ),
        'title' => $subject,
        'description' => array(
            'body' => $details,
        ),
    ),
);

$json = json_encode($array);

Comments

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.