0

I have a series of PHP variables:

$a = "a";
$b = "b";
$c = "c";

and so on...

How can I convert all these variables into one single JSON object? Is this possible?

Victor York
  • 1,621
  • 4
  • 20
  • 55
  • Do you know all the names of all the variables? – Halcyon Mar 04 '14 at 15:11
  • Yes, I know json_encode exists but ive only used this for one variable, can I use the same function for various? – Victor York Mar 04 '14 at 15:12
  • 1
    make an array of your vars , and json encode it – Azrael_404 Mar 04 '14 at 15:13
  • @MONZTAAA Yes. Generally speaking though, if you're storing these vars together, you will want them in a non associative array at LEAST. If each one has a different usage however, e.g. `$a` is a list of apple makers, `$b` is a list of users, then you will want to create an associative array instead. – Sean Mar 04 '14 at 15:17

3 Answers3

4

You probably shouldn't have such a structure. That's what arrays are for.

You're currently storing data as sequential strings. This is a bad idea to begin with. When you have data that can be grouped together, it's almost always better to use an array instead. That makes things a whole lot easier.

Now to answer your original question:


If the number of variables are known, you could simply create an array first and then use json_encode() to create the JSON string:

$arr = array($a, $b, $c);
$json = json_encode($arr);

If the variable names aren't known beforehand, you could use get_defined_vars() to get a list of defined variables.

$arr = array_filter(get_defined_vars(), 'is_string');
echo json_encode($arr);

This is a bad idea though. This would only work if all the variables are to be included in the JSON representation. For example, if you had a private variable $somePrivateData defined in your code, the JSON string will also contain the value of that variable.

Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • That's what I wanted to come up with. To avoid the problem you talked about, you could simply define a new scope using a closure with `call_user_func()` - http://pastie.org/8860516 However I agree with you, using `get_defined_vars()` usually should make your alarm bells ring. – thpl Mar 04 '14 at 15:23
  • 1
    @ThomasDavidPlat: Why, save the trouble and use an array instead. Problem solved! – Amal Murali Mar 04 '14 at 15:23
  • True that. Just edited my comment. This should not be done. However if we want to match specification of the question that could be a solutions. Going with arrays from the beginning is of course the best of all practices. – thpl Mar 04 '14 at 15:25
  • 1
    @ThomasDavidPlat: Which is why I've made it **bold** :) – Amal Murali Mar 04 '14 at 15:27
  • @AmalMurali I was told to use JSON since I was later going to use ajax to fill in fields with this data. Is this incorrect? – Victor York Mar 04 '14 at 16:03
  • @MONZTAAA: That's absolutely correct. I was talking about the way you're currently storing data. You're now storing it as individual variables. Are you sure you're reading the [latest revision](http://stackoverflow.com/a/22175849/1438393) of my answer? As I've made it bold, you should use **arrays** for this instead. That'll make your life easier! – Amal Murali Mar 04 '14 at 16:05
  • @AmalMurali could we speak in a chat room? – Victor York Mar 04 '14 at 16:10
  • @MONZTAAA: Okay, sure (add another comment to trigger the chat room discussion) – Amal Murali Mar 04 '14 at 16:14
  • @AmalMurali Lets do it :D – Victor York Mar 04 '14 at 16:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48973/discussion-between-amal-murali-and-monztaaa) – Amal Murali Mar 04 '14 at 16:15
1

You can convert a variable into using json_encode() (docs). Typically, this is used by passing an array:

$array = array($a, $b, $c);

$result = json_encode($array);

Edit (from the comments):

If you want to separate each value, you may (as you've mentioned in your comments) call the encode function on each string separately. For example, you may still put them into an array and use

$result = array_map('json_encode', $array);

$result will contain 3 json encoded strings, but the variable itself is still an array.

Of course, you may still use:

$a = json_encode($a);
$b = json_encode($b);
$c = json_encode($c);
Sean
  • 2,278
  • 1
  • 24
  • 45
  • 1
    Yes, thank you! Actually had the PHP documentation link pointing to encode, but had a minor brain fart for the answer line. Good spot! – Sean Mar 04 '14 at 15:13
  • Seems good! I will test it and mark it as good as soon as I test it xD – Victor York Mar 04 '14 at 15:14
  • @Sean I have tried your code but I found an issue. It returns everything into the same string. I would like for each value to be seperate and work some way as an object works. Is this possible? – Victor York Mar 04 '14 at 15:32
  • Edited answer from the comments! – Sean Mar 04 '14 at 15:37
0

Maybe this can help you: json_encode function