encoding - How can I use swedish letters with json_encode() in PHP? -
i have array this, json encode:
$regulararray = array( array( "label" => "hello world", "value" => 1 ), array( "label" => "hej världen", "value" => 2 ) ); $jsonarray = json_encode( $regulararray );
("hej världen" means hello world in swedish) when print $jsonarray
this:
[{"label":"hello world","value":1},{"label":null,"value":2}]
why label null
second item in array? know has word "världen" since contains non-standard letter. how can around this?
json_encode function works utf-8 encoded data. may change input array data utf-8.
encode input array data using utf8_encode , decode whenever need data using utf8_decode
<?php $regulararray = array( array( "label" => "hello world", "value" => 1 ), array( "label" => "hej världen", "value" => 2 ) ); $regulararray[1]['label'] = utf8_encode( $regulararray[1]['label']); echo $jsonarray = json_encode( $regulararray ); $data = json_decode($jsonarray, true); $data[1]['label'] = utf8_decode($data[1]['label']); print_r($data); ?>
output:-
[{"label":"hello world","value":1},{"label":"hej v\u00c3\u00a4rlden","value":2}] array ( [0] => array ( [label] => hello world [value] => 1 ) [1] => array ( [label] => hej världen [value] => 2 ) )
i made test page, works fine.
Comments
Post a Comment