1

code not working

$data['price']='1ドル';
print_r(json_encode($data));

result is

{"price":"null"}

i need result like

{"price":"1ドル"}
BenMorel
37.1k53 gold badges208 silver badges339 bronze badges
asked May 14, 2012 at 11:06
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));
answered May 14, 2012 at 11:13
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 ..

answered May 14, 2012 at 11:09

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.

Ben
21.3k12 gold badges75 silver badges117 bronze badges
answered May 14, 2012 at 11:21

Comments

0

Try this

$data['price'] = htmlentities('1ドル', ENT_COMPAT, 'utf-8');
Pang
10.2k146 gold badges87 silver badges126 bronze badges
answered May 14, 2012 at 11:33

Comments

-2

Try like this:

$data['price']="1ドル";
print_r(json_encode($data));

My result:

{"price":"1ドル"}
Pang
10.2k146 gold badges87 silver badges126 bronze badges
answered May 14, 2012 at 11:14

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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.