1

code not working

$data['price']='£1';

print_r(json_encode($data));

result is

{"price":"null"}

i need result like

{"price":"£1"}
5
  • you are not encoding same array .. check your code.. Commented May 14, 2012 at 11:08
  • 4
    try replacing £ with £ Seems like an Commented May 14, 2012 at 11:08
  • Related: stackoverflow.com/questions/3694346/… Commented May 14, 2012 at 11:12
  • by mistek i have writen $dat i have done $data in my code still its not working Commented May 14, 2012 at 11:16
  • 1
    it's a charset problem ;) just transform the special char to a html entity. it's no nice solution but may be the one with the less work Commented May 14, 2012 at 11:18

5 Answers 5

3

Try this :

$data['price']='£1';
print_r(json_encode($data));
Sign up to request clarification or add additional context in comments.

2 Comments

shouldn't it be £1 technically?
@Ben yes, it should be. As i have tried without ';' I got the same result.. so I think it's not necessary :) thnk you tho
0

You are not encoding same array.. you are storing value in $dat and encoding $data....

check your code ..

1 Comment

He's getting the print_r correctly so he must've made a typo posting his code here
0

I am afraid you cannot encode £ to JSON unfortunately. You can try £ in it's place and it will work.

Comments

0

Try this

$data['price'] = htmlentities('£1', ENT_COMPAT, 'utf-8');

Comments

-2

Try like this:

$data['price']="$1";

print_r(json_encode($data));

My result:

{"price":"$1"}

4 Comments

He wants the pound sign, not dollar sign
sure but i don't have pound on my keyboard :)
@Hataru that's kind of a dumb reply then since $ is on the ascii table and doesn't cause any utf8 json encoding issue..
yes i try now with pound and of course he needs &pound ... thanks

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.