3

I have the following in myfile.php

$json_data = '{"type":"email","msg":"some text"}';

Instead of typing "email" or "some text" I want to concat the php variables $thetype and $themsg to the line before.

How can I do that? No matter what I do I get a syntax error.

I'm trying:

$json_data = '{"type":"+$thetype+","msg":"+$themsg+"}';

But as I say errors galore.

Thanks a lot

2 Answers 2

10

Your question is a little vague...

Are you looking for this?

$json = array('type' => $thetype, 'msg' => $themsg);
$json_data = json_encode($json);

That will set $json_data to a string like what you described:

<?php

$thetype = 'something';
$themsg = 'something else';
$json = array('type' => $thetype, 'msg' => $themsg);
$json_data = json_encode($json);
var_dump($json_data);

Would print:

string(43) "{"type":"something","msg":"something else"}"

See the PHP manual for json_encode.

You could try and build the string by hand, like this:

$json_data = '{"type":"'. addcslashes($thetype,"\"'\n").'","msg":"'. addcslashes($themsg,"\"'\n").'"}';

But, you'll generally be better off using json_encode, as it's designed for this purpose and is much less likely to produce invalid JSON.

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

10 Comments

Is it possible to do anything like: $json_data = '{"type":"+$thetype+","msg":"+$themsg+"}';?
@user523129 Possible, yes. But not advisable. Using Josh's solution, you can be sure to get a correctly formated json string. Otherwise you can easily break the result. E.g. using $thetype = 'break " me';
It is possible, though it's just wrong. In either case, you have to encode data for JSON. In example provided by @Josh, you do it once on the whole array. If you want to do it "your way", you have to do it twice - $jsonType = json_encode($theType); $jsonMsg = json_encode($theMsg); $jsonData = "{\"type\":{$jsonType},\"msg\":{$jsonMsg}}"; - this is wrong as you have to both call json_encode() multiple times and have to manually build a string that is hard to read and modify.
You could... I edited my answer to show how. But this is what json_encode is designed for
@binaryLV I had addcslashes wrong at first. It takes two parameters, I had forgotten the second one which is a list of characters to escape. My example list above is nowhere near complete for valid JSON!!!
|
-1

PHP has a function to encode json. HOWEVER you need to output it as an object to save the keys. This is a 3D array by the way.

$data[]=array('type' => $thetype0, 'msg' => $themsg0);
$data[]=array('type' => $thetype1, 'msg' => $themsg1);
$json=json_encode((object)$data);

EDIT: This method should be used for OLDER PHP versions. It is compatible with PHP 5.3. The json_encode() function has several changes to it that makes it so the object output isnt available in PHP 5.2.

2 Comments

Don't forget the JSON_FORCE_OBJECT option.
-1, This isn't what he's asking... Also, you don't need to convert it to a object

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.