force json_encode not to put quotes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vassy
Forum Newbie
Posts: 1
Joined: Wed Feb 10, 2010 6:10 am

force json_encode not to put quotes

Post by vassy »

Hello.

I need to force json_encode NOT to put any quotes around certain values.
Example:

Code: Select all

 
$ar = array(
'data' => '$(this).val()'
);
 
json_encode returns:

Code: Select all

 
{"data":"$(this).val()"}
 
and $(this).val() doesn't get executed by javascript when I publish this result on a page.

Is it possible to tell json_encode not to enclose some values with quotes?
Like when it doesn't do that for integers.

Thank you!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: force json_encode not to put quotes

Post by pickle »

It's not legitimate/valid JSON unless everything's in quotes - so you're likely not going to get away from that. You could try eval()-ing what needs to be executed as Javascript.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply